The single highest-payoff topic on the exam ā and FRQ 3 is now ArrayList-only.
Home ā Unit 4b
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).
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.
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.
list contain after this code?ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(1, 15);
list.remove(2);
list.set(2, "z") does what?["cat", "cat", "dog"]?for (int i = 0; i < animals.size(); i++) {
if (animals.get(i).equals("cat")) {
animals.remove(i);
}
}
remove() ā elements get skipped. Go backward or i--.set (replace) with add(i, x) (insert-and-shift)..size() vs .length vs .length() ā ArrayList / array / String. Three different things.ArrayList<int> ā primitives can't go in the angle brackets.== on elements that are Strings or Integers ā use .equals().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 →