I have to input 5 int arrays, in less than 3 sec, means execution time should be less than 3,and i was using Scanner class or that,but it is making the execution time more,so is there any other possible way,i have to get input as
我必须输入5个int数组,在不到3秒的时间内,意味着执行时间应该小于3,而我正在使用Scanner类或者那个,但它会使执行时间更长,所以还有其他任何可能的方式,我必须得到输入
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
And have to input 1 2 3 in one line ?how would i do it..? i was taking initially string as input ,and then using split(" ") method for separating it,then parsing it using wrapper classes? any other way to do it?
并且必须在一行中输入1 2 3?我该怎么做...?我把最初的字符串作为输入,然后使用split(“”)方法分离它,然后使用包装类解析它?还有其他办法吗?
1 个解决方案
#1
2
Try the following snippet, I guess its what you may be looking for inputting 2d array without splitting the string using Scanner..
尝试下面的代码片段,我猜你可能正在寻找输入2d数组而不使用Scanner分割字符串。
int[][] array = new int[5][2];
Scanner scan = new Scanner(System.in);
int i=0;
int k=0;
while(scan.hasNextInt()){
array[i][k] = scan.nextInt();
k++;
if(k == 2) {
k = 0;
i++;
}
if(i == array.length){
break;
}
}
for(int p=0; p < array.length; p++) {
for(int j=0;j < 2; j++) {
System.out.print(array[p][j] + " ");
}
}
#1
2
Try the following snippet, I guess its what you may be looking for inputting 2d array without splitting the string using Scanner..
尝试下面的代码片段,我猜你可能正在寻找输入2d数组而不使用Scanner分割字符串。
int[][] array = new int[5][2];
Scanner scan = new Scanner(System.in);
int i=0;
int k=0;
while(scan.hasNextInt()){
array[i][k] = scan.nextInt();
k++;
if(k == 2) {
k = 0;
i++;
}
if(i == array.length){
break;
}
}
for(int p=0; p < array.length; p++) {
for(int j=0;j < 2; j++) {
System.out.print(array[p][j] + " ");
}
}