Skip to content
Codescope

ArrayList vs LinkedList

One is an array that occasionally moves. The other is a chain of boxes scattered across the heap.

core10 min8 steps

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

main()
listArrayList
→ elementData
sizeint
0

Heap

an ArrayList is an array plus a size counter
Object[] elementDatasize 0 · capacity 10
[0]
null
?
[1]
null
?
[2]
null
?
[3]
null
?
[4]
null
?
[5]
null
?
[6]
null
?
[7]
null
?
[8]
null
?
[9]
null
?

size 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.

01 / 08

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.

arraylist-growth.javaJava
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);    }}