The Plain Picture#
ArrayList — a row of lockers that can move to a bigger row when full. Reaching locker #500 is instant. Inserting at the front means everyone shifts one locker along.
LinkedList — a treasure hunt: each clue says where the next one is. Adding a clue in the middle is easy once you're there, but getting to clue #500 means following 500 clues.
The Real Names#
ArrayListstores elements in an internal array (Java Basics Ch 8). Default starting capacity 10; when full, it grows by 1.5× and copies — amortized O(1) add (Big-O and Complexity Ch 7).LinkedListis a doubly linked list: each element sits in a separate node object holding the value and references to the previous and next nodes. (Linked lists as a data structure are taught in Linked Lists (coming soon).)
ArrayList LinkedList ┌─────┬─────┬─────┬─────┬─────┬─────┐ ┌───────┐ ┌───────┐ ┌───────┐ │Intro│Cells│Gene…│ │ │ │ │ Intro │ ⇄ │ Cells │ ⇄ │ Gene… │ └─────┴─────┴─────┴─────┴─────┴─────┘ └───────┘ └───────┘ └───────┘ size = 3, capacity = 6 each node: value + prev + next references one contiguous block → cache-friendly scattered objects → cache misses
Under the Hood#
The List API#
List<String> notes = new ArrayList<>();
notes.add("Cells"); // add at the end
notes.add("DNA");
notes.add(0, "Intro"); // insert at index 0 (shifts others right — O(n))
notes.set(2, "Genetics"); // replace index 2
System.out.println(
notes +
" size=" +
notes.size() +
" get(1)=" +
notes.get(1) +
" indexOf=" +
notes.indexOf("Genetics") +
" contains=" +
notes.contains("DNA")
);
// [Intro, Cells, Genetics] size=3 get(1)=Cells indexOf=2 contains=falseAlso: isEmpty(), remove(int index), remove(Object o), clear(), subList(from, to), sort(comparator), addAll(other).
The remove overload trap (from Classes, Objects and Memory Ch 8):
List<Integer> nums = new ArrayList<>(List.of(10, 20, 30, 1));
nums.remove(1); // removes the element AT INDEX 1 (20) → [10, 30, 1]
nums.remove(Integer.valueOf(1)); // removes the VALUE 1 → [10, 30]ArrayList vs LinkedList — the classic comparison#
| Operation | ArrayList | LinkedList |
|---|---|---|
get(i) / set(i, x) | O(1) | O(n) — walk from an end |
| add/remove at the end | amortized O(1) | O(1) |
| add/remove at the front | O(n) — shift everything | O(1) |
| add/remove in the middle | O(n) shift | O(n) to walk there, then O(1) |
| memory per element | ~4–8 bytes (a reference) | a whole node object (~24+ bytes) + the reference |
| cache behaviour | excellent (contiguous) | poor (scattered nodes — How Computers Work Ch 6) |
In practice, use ArrayList. Even in its "good" cases, LinkedList's node allocation and cache misses usually make it slower, and for adding
and removing at both ends, ArrayDeque (Ch 8) beats it. LinkedList is almost never the right answer in modern Java — and saying so, with the
reasons above, is a strong interview answer.
Fixed and immutable lists#
List.of(1, 2).add(3); // UnsupportedOperationException — List.of is fully IMMUTABLE
List<Integer> fixed = Arrays.asList(1, 2, 3);
fixed.set(0, 9); // allowed → [9, 2, 3]
fixed.add(4); // UnsupportedOperationException — Arrays.asList is FIXED-SIZE (backed by the array)
new ArrayList<>(List.of(1, 2)); // a normal, growable copyList.of also rejects null elements.
Check yourself
4 questionsWhy is ArrayList.get(i) O(1) but LinkedList.get(i) O(n)?
Show answerHide answer
ArrayList computes the address directly from the index; LinkedList must follow node links one by one.
Why is LinkedList usually slower than ArrayList even for operations with the same Big-O?
Show answerHide answer
Each element is a separate object: more memory, more allocation, and scattered nodes cause cache misses.
What does list.remove(1) do on a List<Integer>?
Show answerHide answer
Removes the element at index 1 (use remove(Integer.valueOf(1)) to remove the value 1).
What's the difference between List.of, Arrays.asList and new ArrayList<>(...)?
Show answerHide answer
List.of is immutable; Arrays.asList is fixed-size but settable (backed by an array); new ArrayList<> is fully mutable and growable.