I asked questions in my programming book and came across this question:
What is the output of the following code?
int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; System.out.println("array[0][1] is " + array[0][1]);
The book says the answer is:
array [0] [1] is 2
I found out that resizing an array is not possible. From what I understand, the problem is that
int[][] array = new int[5][6]
creates 5 arrays of 6 elements that would display 0 by default if you displayed it on the console
000000 000000 000000 000000 000000
and now from what I understand is that
array[0] = x;
basically resizes the first array, which has six elements from 0 to an array with two elements: 1 and 2.
What? I do not understand? This is what
array[0] = x;
does it so that it really just changes the index 0 element and the index 1 element of the first array? and saving the index of 2,3,4,5 elements as 0 in the array [0]?
I found this question Resize array while saving current elements in Java? but I donβt think it helps me answer this question.
source share