It is not possible to change the size of the same array. The closest thing you can do is create a new one either with new
operator and System.arraycopy()
the data from the old one, or with Arrays.copyOf()
.
You may also consider using List
instead of array. The size of List
can be changed which might suit your task.
Regarding the second part of the question: two-dimentional array is an array of length let's say N, each item of that is an array of some length too. x[i] = new int[m]
means:
- Creating new
int
array of sizem
; - Setting reference to it into the
x[i]
;
This is essentially the same - new arrays being created, the size is not modified.