Junior — Middle
Is it possible to change the length of an existing array?
sobes.tech AI
Answer from AI
In Java, arrays have a fixed length, which is set at creation and cannot be changed during program execution. If you need to change the length of an array, you must create a new array of the desired size and copy the data from the old array into it.
Example of changing array length:
int[] oldArray = {1, 2, 3};
int[] newArray = new int[5];
System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
// now newArray of length 5 contains elements from oldArray
For dynamic resizing of collections in Java, classes from the java.util package, such as ArrayList, are typically used, which automatically expand when elements are added.