01
Overview

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.

02
Core Concept

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 returns true.
  • void add(int index, E obj) — insert at index; shifts later elements right.
  • E get(int index) — read the element at index.
  • E set(int index, E obj) — replace the element at index; returns the old value.
  • E remove(int index) — remove and return the element at index; 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).

03
Key Vocabulary

ArrayList vocabulary.

Used in every ArrayList-related MCQ and FRQ.

Vocabulary
  • 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 ArrayList index.
04
AP Exam Focus

How ArrayList is tested.

Shifting, removal traps, and array-vs-list confusion dominate.

Exam Focus

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 i and then increment i, you skip the next element.
  • size() vs length. Arrays use arr.length; ArrayLists use list.size().
  • No primitives. ArrayList<int> is a compile error; use ArrayList<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.

05
Worked Example

Remove all matching elements safely.

The most common ArrayList trap and the standard fix.

Worked Example AP-style reasoning

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.

06
Common Mistakes

ArrayList errors you must learn to spot.

A few rules cover most points lost.

Watch Out
  • Using length on an ArrayList. ArrayLists have size(), not length.
  • Using primitives as the generic type. ArrayList<int> won't compile; use ArrayList<Integer>.
  • Removing in a forward loop without adjusting. Either iterate backwards or decrement i after every removal.
  • Bracket access. list[i] is invalid; use list.get(i) and list.set(i, v).
  • Out-of-bounds index. Valid indices are 0 to list.size() - 1.
  • Forgetting the import. ArrayList requires import java.util.ArrayList; (FRQ rubrics generally accept it without).
07
Reference Table

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.
08
Practice Tip

How to build ArrayList fluency.

A handful of patterns covers nearly every FRQ.

Practice Strategy

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 add or remove is 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.

09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

Topic Quiz 40 min 23 marks Pending

AP CSA Topic Practice: ArrayList

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 04 Topic 03 ArrayList FRQ Practice

AP-style topic practice assessment

Start