ArrayList Traversal in AP CSA: Complete Guide (2025-2026)

ArrayList Traversal in AP CSA: Complete Guide (2025-2026)

ArrayList traversal in AP CSA means iterating over every element in an ArrayList to search, filter, accumulate, or modify values, and it is one of the most heavily tested skills in Unit 4 (30–40%). You can traverse an ArrayList with a standard for loop using get(i) and size(), or with an enhanced for loop for read-only traversals. Knowing which loop type to use — and the critical restriction on the enhanced for loop when modifying the list — is essential for FRQ success.

💻 Code Examples — Predict First

Before running each example, write down your prediction. This is the single most effective AP exam study technique.

🤔 Predict the output before running:

Example 1: Sum with Standard for Loop
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList nums = new ArrayList();
        nums.add(3); nums.add(7); nums.add(1); nums.add(9);
        int sum = 0;
        for (int i = 0; i < nums.size(); i++) {
            sum += nums.get(i);
        }
        System.out.println(sum);
    }
}
Running…

20 — Standard for loop: i goes 0 to size()-1. get(i) retrieves each element. sum = 3+7+1+9 = 20.

🤔 Predict the output before running:

Example 2: Enhanced for Loop (Read-Only)
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList names = new ArrayList();
        names.add("Alice"); names.add("Bob"); names.add("Charlie");
        for (String name : names) {
            System.out.println(name);
        }
    }
}
Running…

Alice / Bob / Charlie — The enhanced for loop is cleaner for read-only traversal. Each iteration assigns the next element to 'name'. You CANNOT use this loop when removing elements.

🤔 Predict the output before running:

Example 3: Find Maximum in ArrayList
import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList nums = new ArrayList();
        nums.add(4); nums.add(7); nums.add(2); nums.add(9); nums.add(3);
        int max = nums.get(0);
        for (int i = 1; i < nums.size(); i++) {
            if (nums.get(i) > max) {
                max = nums.get(i);
            }
        }
        System.out.println(max);
    }
}
Running…

9 — Initialize max to get(0), then start loop at i=1. Compare each element to max and update if larger. Pattern identical to array max, just using get() and size().

❌ Common Pitfalls

These are the mistakes students most often make on the AP CSA exam with ArrayList traversal AP CSA. Study them carefully.

1
⚠ Using arr.length instead of list.size()

ArrayLists use .size() (a method call). Using .length on an ArrayList is a compile error. This mix-up is extremely common when switching between arrays and ArrayLists.

for(int i=0; i
  
2
⚠ Using enhanced for loop when modifying the list

The enhanced for loop does NOT allow adding or removing elements while iterating. Doing so throws a ConcurrentModificationException at runtime. Use a standard for loop (with careful index management) when removing elements.

for (Integer v : list) {
    if (v < 0) list.remove(v); // ConcurrentModificationException!
}
3
⚠ Starting max/min at index 0 instead of get(0)

Students sometimes write int max = 0 instead of int max = list.get(0). For a list of all-negative integers, max would incorrectly remain 0.

4
⚠ Off-by-one: using <= size() instead of < size()

Valid ArrayList indices are 0 to size()-1. Using i <= size() causes an IndexOutOfBoundsException on the extra iteration.

🎓 AP Exam Tip

On the AP FRQ section, ArrayList traversal methods always follow this template: (1) declare and initialize result variable, (2) loop from 0 to size()-1, (3) access element with get(i), (4) apply condition, (5) update result, (6) return result. Memorize this structure.

⚠ Watch Out!

For the AP exam: use enhanced for loop only when reading elements. Use standard for loop when you need the index or plan to modify the list. Using the wrong loop type is the single most common ArrayList FRQ structural error.

✍ Check for Understanding (8 Questions)

Your Score: 0 / 0
1. What is the correct loop to traverse all elements of ArrayList list?
2. What does the enhanced for loop NOT allow you to do?
3. What is printed?
ArrayList L = new ArrayList<>();
L.add(2);L.add(4);L.add(6);
int s=0;
for(int v:L) s+=v;
System.out.println(s);
4. What is the correct way to get the LAST element of ArrayList list?
5. How many times does the loop body execute?
ArrayList L = new ArrayList<>();
L.add("a");L.add("b");L.add("c");L.add("d");
for(String s:L) System.out.println(s);
6. Which loop correctly counts elements greater than 5 in ArrayList list?
7. What is the index of the FIRST element in any non-empty ArrayList?
8. Which traversal correctly builds a new ArrayList of only even numbers from list?

❓ Frequently Asked Questions

How do you traverse an ArrayList in AP CSA?

Use a standard for loop: 'for (int i = 0; i < list.size(); i++)' with list.get(i) to access elements. Or use an enhanced for loop: 'for (ElementType e : list)' for simpler read-only traversal.

When should I use enhanced for loop vs standard for loop with ArrayList?

Use enhanced for loop when you only need to read elements. Use a standard for loop when you need the index, plan to modify (add/remove) elements, or need to compare adjacent elements.

What is ConcurrentModificationException?

ConcurrentModificationException occurs when you try to add or remove elements from an ArrayList while iterating with an enhanced for loop. To avoid it, use a standard for loop with careful index management when removing elements.

How do I find the maximum in an ArrayList?

Initialize max to list.get(0), then loop from index 1 to size()-1, comparing each list.get(i) to max and updating if larger. This is identical to the array max pattern but uses get() and size() instead of arr[i] and arr.length.

What is the difference between ArrayList size() and array length?

ArrayList uses the size() method (with parentheses). Arrays use the length field (no parentheses). Mixing these up causes a compile error. This distinction is tested on every AP exam.

TC

Tanner Crow — AP CS Teacher & Tutor

11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.

  • 54.5% of students score 5 on AP CSA (national avg: 25.5%)
  • 1,845+ verified tutoring hours • 5.0 rating
  • Free 1-on-1 tutoring inquiry: Wyzant Profile

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com