Array assignment versus loop assignment

In the class I have a private array

private boolean[][][] array;

which is later declared as

array = new boolean[2][100][100]; //*

At some point, I want to rewrite the first array in the first dimension with the second array of the first dimension. I suggested this should work

array[0] = array[1];

but this led to misbehavior. I tried this simple loop:

for (int column = 0; column < array[0].length; column++) {
    for (int row = 0; row < array[0][0].length; row++) {
        array[0][column][row] = array[1][column][row];
    }
}

and it worked as expected.

Why didn't the first code snippet work?

* The first dimension is static at 2, and the other from another array. I deleted them for clarity.

+4
source share
3 answers

, , . - , . ( ) .

, :

Reassignment

100x100 100x100 1. .

, 1, :

array[0] = array[1];
array[1] = new boolean[100][100];
+5

array[0] = array[1];

, , array[1], array[0]. Java , array[0] array[1] .

/ , : for, while, do-while, , .

+3

, , , " ".

-

array[0]=array[1];

array[0][1]=array[1][1];

,

array[0][0][0]=array[1][0][0];

, , , .

-1

Source: https://habr.com/ru/post/1612161/


All Articles