ArrayList vs LinkedList
One is an array that occasionally moves. The other is a chain of boxes scattered across the heap.
Before you start
You reach for ArrayList when you need a list that grows. But an array under the hood has a fixed size — so how does the list appear to keep growing forever?
The trick is that ArrayList keeps two numbers: size (how many elements you added) and capacity (how much room the internal array has). When you exceed capacity, it quietly allocates a bigger array (typically 1.5× or 2× the old one), copies everything over, and keeps going.
This lesson watches capacity double as elements pile up, and compares against LinkedList — a chain of scattered heap nodes with completely different trade-offs.
Stack
Heap
an ArrayList is an array plus a size countersize 0, capacity 10 once the first add triggers allocation.
Step 1 of 8. An ArrayList is an array and a counter. `new ArrayList<>()` does not allocate storage yet. It holds a shared empty array and waits until you actually add something. The two things it tracks are the backing array and `size` — how many slots are in use. Capacity and size are different numbers, and confusing them is the source of most surprises here.
Step 01 / 08
An ArrayList is an array and a counter
new ArrayList<>() does not allocate storage yet. It holds a shared empty array and waits until you actually add something.
The two things it tracks are the backing array and size — how many slots are in use. Capacity and size are different numbers, and confusing them is the source of most surprises here.
import java.util.ArrayList;import java.util.LinkedList;public class Main { public static void main(String[] args) { ArrayList<String> list = new ArrayList<>(); list.add("a"); // lazily allocates capacity 10 list.add("b"); list.add("c"); list.get(1); // O(1): one index calculation list.add(0, "z"); // O(n): shifts everything right list.remove(0); // O(n): shifts everything left ArrayList<String> sized = new ArrayList<>(1000); }}