简体   繁体   English

如何用二维创建二维网格。 阵列?

[英]How to create two dimensional grid with two-d. array?

This is my current code: 这是我目前的代码:

public static void main(String[] args) {
    int [][] twoD = new int [5][5];

    /*for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            System.out.print(twoD[i][j] + "");
        }
    }*/

 }   
}

I can't seem to do it. 我似乎无法做到这一点。 I got confused and I removed the part of testing w/commenting. 我感到困惑,我删除了测试/评论部分。 Just ignore that. 只是忽略它。

I am aiming to get a two dimensional array like this: 我的目标是获得这样的二维数组:

1 2 3 4 5 1 2 3 4 5

2 4 6 8 10 2 4 6 8 10

3 6 9 12 15 3 6 9 12 15

4 8 12 16 20 4 8 12 16 20

5 10 15 20 25 5 10 15 20 25

However, I just don't get it. 但是,我只是不明白。 How can I get that result? 我怎样才能得到这个结果? I'm a beginner at java. 我是java的初学者。

First, you need to populate the array with data, and you forgot System.out.println for each row of the array. 首先,您需要使用数据填充数组,并且您忘记了数组的每一行的System.out.println

int [][] twoD = new int [5][5];

// populate array with data
for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        twoD[i][j] = (j+1)*(i+1);
    }
}

// print result
for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        System.out.print(twoD[i][j]);
        System.out.print(" ");
    }            
    System.out.println();
}

You have to populate the data as well: 您还必须填充数据:

int[][] arr = new int [5][5];

for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        arr[i][j] = (j+1)*(i+1);
    }
}

And the code to print would be: 要打印的代码是:

for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        System.out.print(arr[i][j] + " ");
    }            
    System.out.println();
}

You are doing fine, you just need to put line jump System.out.println(); 你做得很好,你只需要把线跳转System.out.println(); every time the second for ends 每次第二次结束

for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            System.out.print(twoD[i][j] + " ");
        }
        System.out.println();
    }

You are on the right track, that for loop will print out the array like that, all you need to do is print a new line character after finishing the for(j) loop. 你是在正确的轨道,for循环将打印出这样的数组,你需要做的就是在完成for(j)循环后打印一个新的行字符。 But, at least in the snippet you posted, you aren't actually doing any assignments, so there aren't any values in your array to print, Java will initialize all ints to zero for you. 但是,至少在您发布的代码段中,您实际上并没有进行任何分配,因此您的数组中没有任何值可以打印,Java会将所有整数初始化为零。

The array doesn't just automatically populate with incrementing integers, rather each cell of the array will automatically initialized to 0, you have to set the values you want the array to contain. 数组不仅会自动填充递增的整数,而是数组的每个单元格将自动初始化为0,您必须设置希望数组包含的值。 You can use the concept of your testing class to do this if you wish, just set each cell of the 2D array to a certain value. 如果您愿意,可以使用测试类的概念来执行此操作,只需将2D数组的每个单元格设置为特定值即可。 After that, you can print out the array, making sure to print a each row of the array on a new line. 之后,您可以打印出阵列,确保在新行上打印阵列的每一行。 For instance: 例如:

public static void main(String[] args) {
    int [][] twoD = new int [5][5];
    int increment = 1;

    for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            twoD[i][j] = increment++;
        }
    }
    for(i = 0; i<5; i++){
        for(j = 0; j<5; j++){
            System.out.print(twoD[i][j] + " ");
        }
        System.out.println();
    }
}

The first set of nested for loops will set each of the cells of the 2D array to the incremented integers you want (note increment++ will first set the cell to the value increment currently is, then add one to the variable). 第一组嵌套的for循环将2D数组的每个单元格设置为您想要的递增整数(注意increment++将首先将单元格设置为当前的值increment ,然后将一个添加到变量中)。 The second set of nested for loops will print out the array as you desire. 第二组嵌套for循环将根据需要打印出数组。

refer this code 参考这段代码

    int[][] twoD = new int[5][5];

    // add values to array
    for (int i = 0; i < 5; i++) {
        int val = 1;
        val = val + i;
        for (int j = 0; j < 5; j++) {
            twoD[i][j] = val * (j + 1);;

        }

    }

    // Print array
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {

            System.out.print(twoD[i][j] + "   ");

        }

        System.out.println();
    }

as others pointed out you need to print it nicely to see the pattern, i and j are indices of your array. 正如其他人指出你需要很好地打印它以查看模式,i和j是你的数组的索引。 However, I see that you have a nice pattern so just running two loops won't solve the problem. 但是,我看到你有一个很好的模式,所以只运行两个循环将无法解决问题。 Maybe something like this would help (not giving exact answer intentionally) 也许这样的事情会有所帮助(没有故意给出确切的答案)

int [][] twoD = new int [5][5];
int i;
// initialize
int c = 1; int j = 0;
for(c=1; c<5; c++) {
for( i = 1; i<=5; i++){        
        twoD[i-1][c-1] = c*c*i; twoD[c-1][i-1]=c*c*i;
    }
}

for( i = 0; i<5; i++) {    
   for( j = 0; j<5; j++) {   
     System.out.print(twoD[i][j]);System.out.print(" " );
   }
   System.out.println("\n");
}

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

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