Triggering a Resize: When Capacity Is Exhausted
What you'll learn: You'll understand exactly when ArrayList triggers its internal array growth and what causes the resize operation.
The Moment of Exhaustion
Remember that ArrayList maintains two numbers: size (how many elements you've actually added) and capacity (total slots available in the backing array). Most of the time, adding an element simply places it in the next available slot. But what happens when size equals capacity?
When you call add() and there's no room left, ArrayList cannot complete your request with the current array. The backing array is full—every slot is occupied, and arrays cannot grow dynamically in Java.
The Resize Trigger
The resize is triggered at the exact moment when:
size == capacity
Before placing your new element, ArrayList checks if there's space. If not, it must:
- Create a larger array (using the 1.5x growth strategy)
- Copy all existing elements (via
System.arraycopy) - Replace the old backing array with the new one
- Then add your element to the freshly expanded array
Analogy: The Full Parking Lot
Think of a parking lot with 10 spaces (capacity = 10) that currently has 10 cars (size = 10). When the 11th car arrives, you can't just squeeze it in. You must build a bigger lot (say, 15 spaces), move all 10 cars over, then park the new arrival.
This resize doesn't happen on every add()—only when capacity runs out.
Key Takeaway: A resize is triggered when size equals capacity and you attempt to add another element, forcing ArrayList to allocate a larger backing array before completing the insertion.