简体   繁体   English

如何快速输入整数二维数组

[英]How to input integer 2d array fast

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 我必须在不到3秒的时间内输入5个int数组,这意味着执行时间应该少于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..? 并且必须在一行中输入1 2 3?我该怎么办..? i was taking initially string as input ,and then using split(" ") method for separating it,then parsing it using wrapper classes? 我最初将字符串作为输入,然后使用split(“”)方法将其分离,然后使用包装器类对其进行解析? any other way to do it? 还有其他方法吗?

Try the following snippet, I guess its what you may be looking for inputting 2d array without splitting the string using Scanner.. 尝试以下代码段,我想它可能是您在寻找使用Scanner拆分字符串的情况下输入2d数组的内容。

    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] + " ");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM