Java: how arrays work

I worked with relatively large arrays Stringtoday. (Approximately 400 x 400) I was wondering how to make one array equal to another. For instance,

String[][] array1 = new String[400][400];
String[][] array2 = array1;

Does one array equal to another the same as loop through each element and makes it equal to the corresponding position in another array? (As shown below)

for(int y = 0; y < 400; y++) {
    for(int x = 0; x < 400; x++) {
        array2[x][y] = array1[x][y];
    }
}

Now the looping method is similar to how one array is equal to another? Or is the first / second faster than the other? Personally, I believe that the former would be faster just because there is no recursion or the need to manually allocate memory for array2before recursion. But I don’t know where to start looking for this information, and I would like to understand the logic of how Java handles such things.

+5
source share
4

, : , array2 array1, . , array2, "" array1 . , clone(); , , .. ( 2- , ).

+12

array2 = array1 , . array2 array1 .

:

String[][] array1 = new String[4][4];
array1[0][0] = "some string";
String[][] array2 = array1;
array1[0][0] = "another string";
System.out.println("array2: " + array2[0][0]);
array2[0][0] = "a third string";
System.out.println("array1: " + array1[0][0]);
+5

, , , . :

ARRAY 1 - * [] [] [] []... [] [] * - Array 1
Array 2 - [] [] [] []... [] [] Array 2

Then the task of array 1 = array 2 Array 1 Just change its reference to and and begin to read using the memory reference &

+1
source

The second line in the first code example is the creation of a reference variable of type String [], which refers to the same two-dimensional array object to which the reference array1 refers. There is only one two-dimensional array in memory, but two reference variables are related to it: array1 and array2.

You seem to be confused in the definition of recursion, so I will point you here to Recursion in Computer Science .

+1
source

All Articles