简体   繁体   English

使用增强的 for 循环在 Java 中填充多维数组

[英]Filling a multidimensional array in Java using enhanced for loop

I hope you all are doing well, so the problem I guess is in understanding the difference between those two codes:我希望你们都做得很好,所以我想问题在于理解这两个代码之间的区别:

first code (filling multidim-array with enhanced for loop) - does not work第一个代码(用增强的 for 循环填充 multidim-array) - 不起作用

static char[][] battleBoard = new char[10][10];
public static void main(String[] args)
{


    for(char[] rows:battleBoard)
    {
        for(char column:rows)
        {
            column = '*';
        }
    }


}

if we tried to print-out the array (expecting '*' to be assigned to each element in the array) using the following code - we fail (ie it is still empty)!如果我们尝试使用以下代码打印出数组(期望将 '*' 分配给数组中的每个元素) - 我们失败(即它仍然是空的)!

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


    }

But on the other hand we have(it works fine when printed):但另一方面,我们有(打印时效果很好):

static char[][] battleBoard = new char[10][10];
public static void main(String[] args)
{


    for(char[] rows:battleBoard)
    {
        Arrays.fill(rows, '*');
    }

So the question is: does not column, rows in the previous code-blocks stand for new variables - ie Arrays.fill(rows,'*') shouldn't have met the need cause it fills ((rows char array)) but not ((battleBoard char array)) ;所以问题是:前面的代码块中的列、行不代表新变量 - 即Arrays.fill(rows,'*')不应该满足需要,因为它填充 ((rows char array)) 但是不是 ((battleBoard 字符数组)) ;

Thx in advance for helping me out!提前感谢帮助我!

The first method does not fill the array, because in the code第一种方法不会填充数组,因为在代码中

for (char column : rows) {
    column = '*';
}

the value in the array is copied to the new variable column .数组中的值被复制到新的变量column You then just assign a new value for this variable, but the element in the array remains unchanged.然后您只需为该变量分配一个新值,但数组中的元素保持不变。

The method Arrays.fill() does not copy the value, instead it operates directly on the array.方法Arrays.fill()不复制值,而是直接对数组进行操作。 You can do so yourself with this code (which is basically the same as Arrays.fill() does):您可以使用以下代码Arrays.fill()Arrays.fill()基本相同):

for (int i = 0; i < battleBoard.length; i++) {
    for (int j = 0; j < battleBoard[i].length; j++) {
        battleBoard[i][j] = '*';
    }
}

I'd like to thank SilverNak for his answer.我要感谢 SilverNak 的回答。 Here is my final view of the answer (detailed) - for the ones who are searching:这是我对答案的最终看法(详细) - 对于正在搜索的人:

the difference between column and rows is that "column" is a single primitive variable that represents a cell in the multidimensional array (say: battleBoard), but when it comes to "rows" it is a reference variable to an object (the one-dimensional array (say: battleBoard[i]) where i belongs to the interval [0, battleBoard.length), and when it comes to different references x,y,z where y=x, z=y, then any change to be done on one of them(say z) -except for putting it to null- would affect all the others because they refer to the same object.列和行之间的区别在于“列”是表示多维数组中的一个单元格的单个原始变量(例如:battleBoard),但是当涉及到“行”时,它是一个对象的引用变量(一个-维数组(比如:battleBoard[i]),其中 i 属于区间 [0,battleBoard.length),当涉及到不同的引用 x,y,z 其中 y=x, z=y 时,则任何更改为在其中一个(比如 z)上完成 - 除了将其设置为 null - 会影响所有其他人,因为它们引用同一个对象。 I've made a simple analogy with the following 2-code blocks:我用以下 2 个代码块做了一个简单的类比:

first is when we use primitive variables (like "column" in our previous question)首先是当我们使用原始变量时(如上一个问题中的“列”)

int x = 3;
int y = 4;

y = x;
y = 10;
System.out.print(x + " ");
System.out.println(y);

The result would be printing 3 10结果将是打印3 10

-- Notice: changing one does not change the other because they are INDEPENDENT COPIES of each other. -- 注意:改变一个不会改变另一个,因为它们是彼此的独立副本。

second is when we use reference variables (like "rows" in our previous question)第二个是当我们使用引用变量时(比如我们上一个问题中的“行”)

int[] test1 = new int[5];
int[] test2 = test1;
test2[2] = 3;
System.out.print(Arrays.toString(test1) + " ");
System.out.print(Arrays.toString(test2));

The result would be printing [0, 0, 3, 0, 0] [0, 0, 3, 0, 0]结果将打印[0, 0, 3, 0, 0] [0, 0, 3, 0, 0]

--Notice: changing one does change the other because the actual change was done to the object (array in our case); --注意:更改一个确实会更改另一个,因为实际更改是对对象(在我们的示例中为数组)进行的; and test1, test2 are merely references to the same object(array) and they are NOT INDEPENDENT COPIES of each other.和 test1, test2 只是对同一个对象(数组)的引用,它们不是彼此的独立副本。

I think the answers here are waaaaaaay to complicated.我认为这里的答案太复杂了。 The simplest reason is that in the first code you are using a for-each loop.最简单的原因是在第一个代码中您使用了 for-each 循环。 The purpose of this loop is to get you a reference to each element in the array one at a time.这个循环的目的是让您一次一个地引用数组中的每个元素。 So when you say所以当你说

for(char column:rows){
    column = '*';
}

column is a variable that displays the value of the current element and does not actually give you access to write to that element in the array. column是一个变量,它显示当前元素的值,但实际上并不授予您写入数组中该元素的权限。 If you wanted it to work you should instead reverse your loops, use the for-each to print and the double for-loop to write to the array.如果你想让它工作,你应该反转循环,使用 for-each 打印和双 for 循环写入数组。 Also Arrays.fill() simply does what it says which is why it works.此外Arrays.fill()只是按照它所说的去做,这就是它起作用的原因。

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

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