Sobes.tech
Junior — Middle

When increasing the size of an ArrayList, is block copying of the original array's elements performed?

sobes.tech AI

Answer from AI

When increasing the size of an ArrayList in Java, a new larger array is created, and elements from the old array are copied into the new one.

The copying is performed using the system call System.arraycopy(), which implements block copying (similar to memcpy), meaning it copies a block of memory in its entirety, not element by element.

This ensures high performance when expanding the ArrayList, as the copying is efficiently handled at the native code level.

Example from ArrayList code:

int newCapacity = oldCapacity + (oldCapacity >> 1); // increase by 50%
Object[] newElementData = new Object[newCapacity];
System.arraycopy(elementData, 0, newElementData, 0, size);
elementData = newElementData;
When increasing the size of an ArrayList, is block… - sobes.tech