ArrayList: a resizable, object-friendly list.
When you don't know in advance how many items you'll store — or you need to add and remove freely — ArrayList is the AP CSA collection of choice.
An ArrayList is a class from java.util that stores a sequence of object references and grows automatically as you add elements. Unlike arrays, its size changes over time.
ArrayList is a generic type: you specify the element type in angle brackets — ArrayList<String>, ArrayList<Integer>, ArrayList<Book>. Because the type must be an object, primitives use wrapper classes (Integer, Double) and benefit from autoboxing.
On the AP exam, you must know the standard ArrayList methods, the import line, and the common patterns for traversal, search, and removal — including the classic "removing while iterating forward" trap.
The AP CSA ArrayList method subset.
Six methods cover almost every problem.
Import and create
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<String>();
The angle brackets specify the element type. For primitives, use wrapper classes: ArrayList<Integer> rather than ArrayList<int>.
AP CSA ArrayList methods
int size()— number of elements.boolean add(E obj)— append to the end; always returnstrue.void add(int index, E obj)— insert atindex; shifts later elements right.E get(int index)— read the element atindex.E set(int index, E obj)— replace the element atindex; returns the old value.E remove(int index)— remove and return the element atindex; shifts later elements left.
Examples
ArrayList<String> list = new ArrayList<String>();
list.add("apple"); // [apple]
list.add("banana"); // [apple, banana]
list.add(1, "blueberry"); // [apple, blueberry, banana]
list.set(0, "apricot"); // [apricot, blueberry, banana]
String x = list.get(2); // "banana"
list.remove(0); // [blueberry, banana]
int n = list.size(); // 2
Autoboxing and unboxing
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(7); // autoboxing: int 7 -> Integer
int first = nums.get(0); // unboxing: Integer -> int
Java converts between int and Integer automatically — but be aware that the list actually stores Integer objects.
Traversing an ArrayList
// indexed
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// enhanced for
for (String s : list) {
System.out.println(s);
}
Use list.size() as the bound (note the parentheses — it's a method, not a field).
ArrayList vocabulary.
Used in every ArrayList-related MCQ and FRQ.
- ArrayList — a resizable, object-only list from
java.util. - Generic type — the element type specified in angle brackets.
- Wrapper class — object equivalent of a primitive (
Integer,Double). - Autoboxing — automatic conversion from primitive to wrapper.
- Unboxing — automatic conversion from wrapper to primitive.
- size() — number of elements currently stored.
- Index — zero-based position of an element.
- IndexOutOfBoundsException — runtime error for an invalid
ArrayListindex.
How ArrayList is tested.
Shifting, removal traps, and array-vs-list confusion dominate.
MCQ patterns to expect:
- Shifting on insert / remove.
add(i, x)shifts later elements right;remove(i)shifts them left. - Removal while iterating forward. If you remove element
iand then incrementi, you skip the next element. - size() vs length. Arrays use
arr.length; ArrayLists uselist.size(). - No primitives.
ArrayList<int>is a compile error; useArrayList<Integer>.
FRQ tip: when removing matching elements, loop backwards from size() - 1 down to 0. That way, removals never shift elements you haven't visited yet.
Remove all matching elements safely.
The most common ArrayList trap and the standard fix.
Problem. Given an ArrayList<Integer>, remove every occurrence of 0.
Buggy forward version (skips elements):
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 0) {
list.remove(i); // shifts later elements left, but i still increments
}
}
If the list is [0, 0, 1], the loop removes index 0, leaving [0, 1]. Then i becomes 1, skipping the new index 0 (also a 0). Final result: [0, 1] — wrong.
Fix: iterate backwards. Removing from the end doesn't shift any element you haven't visited yet.
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 0) {
list.remove(i);
}
}
Trace. For [0, 0, 1], indices visited are 2, 1, 0. Element 2 is 1 (no removal). Element 1 is 0 (removed → [0, 1]). Element 0 is 0 (removed → [1]). Final result: [1] — correct.
Why this matters. This trap appears on nearly every AP exam in some form. The "remove while iterating" pattern is essential.
ArrayList errors you must learn to spot.
A few rules cover most points lost.
- Using
lengthon an ArrayList. ArrayLists havesize(), notlength. - Using primitives as the generic type.
ArrayList<int>won't compile; useArrayList<Integer>. - Removing in a forward loop without adjusting. Either iterate backwards or decrement
iafter every removal. - Bracket access.
list[i]is invalid; uselist.get(i)andlist.set(i, v). - Out-of-bounds index. Valid indices are
0tolist.size() - 1. - Forgetting the import.
ArrayListrequiresimport java.util.ArrayList;(FRQ rubrics generally accept it without).
AP CSA ArrayList method reference.
The complete tested subset.
ArrayList Reference
AP Quick Reference| Method | Effect | AP Exam Tip |
|---|---|---|
size() |
Returns number of elements | Use as the loop bound (with parentheses). |
add(E obj) |
Appends to the end | Returns true; result usually ignored. |
add(int i, E obj) |
Inserts at index i |
Shifts later elements right. |
get(int i) |
Returns element at i |
Invalid index throws exception. |
set(int i, E obj) |
Replaces element at i |
Returns the old value. |
remove(int i) |
Removes element at i |
Shifts later elements left. |
How to build ArrayList fluency.
A handful of patterns covers nearly every FRQ.
Use the three practice tools below this lesson in order:
- MCQ Practice — for each problem, write the list contents after every operation. Track size changes carefully when
addorremoveis called. - Java Lab — practise inserting, removing, replacing, and traversing. Always use
list.size()as the loop bound. - FRQ Practice — for any "remove matching" problem, iterate backwards. For any "modify in place" problem, prefer the indexed loop with
set.
If you can write the backward-removal loop from memory, you've beaten the most common AP ArrayList trap.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.