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.
📄 Table of Contents
💻 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:
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);
}
}
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:
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);
}
}
}
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:
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);
}
}
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.
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
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!
}
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.
Valid ArrayList indices are 0 to size()-1. Using i <= size() causes an IndexOutOfBoundsException on the extra iteration.
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.
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)
list?
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);
ArrayList L = new ArrayList<>();
L.add("a");L.add("b");L.add("c");L.add("d");
for(String s:L) System.out.println(s);
❓ Frequently Asked Questions
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.
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.
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.
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.
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.
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
🔗 Related Topics
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com