java中二维数组的增强for循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18931332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:37:10  来源:igfitidea点击:

Enhanced for loop for 2d array in java

javaforeachmultidimensional-array

提问by Devraj Jaiman

int Site[][] = new int[N][N];
   for(int[] i : Site)
       for(int j:i)
           Site[i][j]=1;

This code is showing some error. Please correct the code.

此代码显示一些错误。请更正代码。

回答by Patricia Shanahan

int Site[][] = new int[N][N];
for (int[] i : Site)
  for (int j : i)
    i[j] = 1;

The elements of Site are of type int[], so that has to be the type of the first index variable, i. That gives you a reference to an int[], so index into that to access the individual element.

Site 的元素是 int[] 类型,因此它必须是第一个索引变量的类型,即。这为您提供了对 int[] 的引用,因此可以对其进行索引以访问单个元素。

The code could be made clearer by the use of a more meaningful identifier for the out loop index:

通过为循环索引使用更有意义的标识符,可以使代码更清晰:

int Site[][] = new int[N][N];
for (int[] siteRow : Site)
  for (int i : siteRow)
    siteRow[i] = 1;

回答by Mateusz

If you want to use for(int[] i : Site)and for(int j:i), you should refer to the element as j = 1, not Site[i][j]=1;because i's type is int[]and jtakes values of its elements.

如果您想使用for(int[] i : Site)and for(int j:i),您应该将元素称为j = 1,而不是Site[i][j]=1;因为i的类型是int[]并且j采用其元素的值。

回答by jason

You can not use a for-each loop to modify the values stored in the array. Use a regular for loop for that.

您不能使用 for-each 循环来修改存储在数组中的值。为此使用常规 for 循环。

回答by SpicyWeenie

I use this to print the positions of each element of a 2D array with an enhanced for-loop:

我使用它来打印具有增强的 for 循环的 2D 数组的每个元素的位置:

int[][] grid = new int[3][3];
    String divider = "------------------";

    int y = 0;

    for (int[] row : grid)
    {   
        for (int dividers : row)
            System.out.print(divider);

        System.out.println();

        int x = 0;
        for (int columns : row)
        {
            row[columns] = y;
            System.out.print("| (row: " + row[columns] + " col: " + x + ") ");
            x++;
        }   
        y++;
        System.out.println("| ");   
    }

    for (int[] lastDivider : grid)
        System.out.print(divider);

回答by tilak

In your problem using the previous solution we come across another problem like,

在您使用先前解决方案的问题中,我们遇到了另一个问题,例如,

0 1 1

0 1 1

0 1 1

0 1 1

0 1 1

0 1 1

That is first element of the array gets zero every time we print the value of elements. Now, the solution that will resolve your problem is to use the control statements like,

也就是说,每次我们打印元素的值时,数组的第一个元素都会变为零。现在,解决您的问题的解决方案是使用控制语句,例如,

for(int[] row: B)   
        { 
            for(int j: row)
            {
                if(j==0)
                    System.out.print("  "+row[j]);  
            else
                    System.out.print("  "+j);

            }
            System.out.println();
        }

This will definitely solve the problem of first element being ZERO.

这肯定会解决第一个元素为零的问题。

回答by Andrew

int[][] twodim = new int[4][3];

for (int[] row : twodim) {
    for (int j = 0; j < row.length; j++) {
        row[j] = 1;
    }
}

for (int[] row : twodim) {
    for (int j : row) {
        System.out.println(row[j]);
    }
}