Resizing a 2d array in java -
i have been doing questions in programming book , came across question:
what output of following code?
int[][] array = new int[5][6]; int[] x = {1, 2}; array[0] = x; system.out.println("array[0][1] " + array[0][1]); the book says answer is:
array[0][1] 2
i learned far resizing array isn't possible. understand of problem that
int[][] array = new int[5][6] is creating 5 arrays of 6 elements display 0's default if displayed on console
000000 000000 000000 000000 000000 and understand
array[0] = x; is resizing first array has 6 elements of 0 array 2 elements: 1 , 2.
what not understanding?
array[0] = x; is making it's changing index 0 element , index 1 element of first array? , keeping index 2,3,4,5 elements 0's in array[0]?
i found question resize array while keeping current elements in java? don't think helps me answer question.
this line
array[0] = x; is not resizing array array[0]; it's replacing array array[0] such array now
12 000000 000000 000000 000000 the old array[0] discarded , garbage collected. array[0] , x refer same array object, {1, 2}.
Comments
Post a Comment