Unit 4b: ArrayList

The single highest-payoff topic on the exam — and FRQ 3 is now ArrayList-only.

Unit 4 (Data Collections) is 30–40% of the exam

Home → Unit 4b

The method table you need cold

ArrayList<String> list = new ArrayList<>();
list.add("a");          // append to the end → [a]
list.add(0, "b");       // insert at index 0, shifts right → [b, a]
list.get(1)             // "a" — read an element
list.set(1, "c");       // replace → [b, c]  (returns the old value!)
list.remove(0);         // delete index 0, shifts LEFT → [c]
list.size()             // 1 — parentheses! (arrays use .length)

Key differences from arrays: ArrayList grows and shrinks, uses methods instead of brackets, and holds objects (use Integer and Double, not int/double, in the angle brackets).

THE trap: removing while traversing

When you remove(), everything after shifts left. March forward blindly and you skip the element right after every removal:

// BUGGY — skips elements after each removal:
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).equals("x")) list.remove(i);   // next item slides into i… then i++ jumps over it
}

// FIX 1: back up after removing
if (list.get(i).equals("x")) { list.remove(i); i--; }

// FIX 2: traverse BACKWARD — shifts can't hurt you
for (int i = list.size() - 1; i >= 0; i--) { ... }

This exact bug has appeared on the exam in some form for years. If an MC answer says "some elements are skipped," that's usually the one.

Why FRQ 3 matters so much now

On the revised exam, FRQ 3 (Data Analysis) uses ArrayList only — no more plain-array version. Combined with Unit 4's 30–40% weight, ArrayList fluency is the single best investment of your study time.

Try it — exam-style questions

1. What does list contain after this code?
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(1, 15);
list.remove(2);
2. list.set(2, "z") does what?
3. This code is meant to remove every "cat" — what actually happens with ["cat", "cat", "dog"]?
for (int i = 0; i < animals.size(); i++) {
    if (animals.get(i).equals("cat")) {
        animals.remove(i);
    }
}
4. Which declaration is correct for a list of whole numbers?

⚠ Common mistakes that cost points

FRQ 3 is the most learnable question on the exam.

It follows patterns. I'll teach you the patterns, grade your practice like a real reader, and show you exactly where the points are.

Ask about tutoring →