Junior — Middle
How does automatic resizing of an array occur when adding elements in Java?
sobes.tech AI
Answer from AI
In Java, arrays have a fixed size determined at creation. To dynamically change the size, wrapper classes like ArrayList are used.
ArrayList internally stores a regular array. When a new element is added and the internal array is full, a new larger array (usually 1.5 to 2 times bigger) is automatically created, and all elements are copied into it. This allows the collection to expand dynamically without explicit size management.
Example:
ArrayList<String> list = new ArrayList<>();
list.add("one");
list.add("two");
// When elements are added, the size of the internal array increases automatically
Thus, the automatic size change occurs not directly with the array, but with dynamic collections that manage the internal array.