I started to learn JAVA and am trying to understand something about arrays. So within the code below, the elements of arr1
has been assigned to arr2[1]
and in the second loop, arr2[row][col]
was incremented.
I was just monkeying around with debugger and realized that the content of arr1
has also been incremented along with arr2[row][col]
.
It might be a dummy question but I really could not understand it, can you elaborate the logic there please?
public static void main(String[] args){
int[] arr1 = { 1, 2, 3, 4};
int[][] arr2 = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
arr2[1] = arr1;
for (int row = 0; row < arr2.length; row++) {
for (int col = 0; col < arr2[row].length; col++){
System.out.print( arr2[row][col] + " " );
arr2[row][col]++;
}
System.out.println("");
}
}