What to practice
- Trace all six AP CSA ArrayList<E> methods including return values of set(i, obj) and remove(i).
- Identify and fix the forward-loop index-shift bug when removing elements from an ArrayList.
- Write complete ArrayList manipulation methods for AP CSA FRQ #3.
Current CED topics: 4.7, 4.8, 4.9, 4.10
Current learning objectives: 4.7.A, 4.8.A, 4.9.A, 4.10.A
1. Core Concept Instruction
An ArrayList<E> is a dynamically resizing list of object references (primitives must use wrapper types like ArrayList<Integer> or ArrayList<Double>). Master the six AP Exam Quick Reference ArrayList methods: int size(), boolean add(E obj), void add(int index, E obj) (inserts at index and shifts elements at index..size-1 one position right), E get(int index), E set(int index, E obj) (replaces element at index and returns the overwritten element), and E remove(int index) (removes and returns the element at index, shifting elements at index+1..size-1 one position LEFT). CRITICAL EXAM TRAP: Because list.remove(i) shifts all subsequent elements one index to the left, removing elements in a forward loop for (int i = 0; i < list.size(); i++) causes the next element (now shifted into index i) to be SKIPPED when i++ runs! Fix this either by decrementing i-- after remove(i), or by traversing backward from i = list.size() - 1 down to 0.
2. Worked Examples
Worked ArrayList Forward-Removal Skip Bug vs. Correct Removal
Suppose ArrayList<String> words contains ["cat", "dog", "bat", "rat", "ant"]. Trace what remains in words if we attempt to remove all 3-letter words containing "a" using:
for (int i = 0; i < words.size(); i++) {
if (words.get(i).indexOf("a") >= 0) {
words.remove(i);
}
}
- Step 1 (i = 0): words.get(0) is "cat", which contains "a". words.remove(0) removes "cat" and shifts everything left: words is now ["dog", "bat", "rat", "ant"]. Then i increments to 1.
- Step 2 (i = 1): words.get(1) is "bat" (notice "dog" at index 0 was skipped, though "dog" had no "a"). "bat" contains "a", so words.remove(1) removes "bat" and shifts "rat" into index 1: words is now ["dog", "rat", "ant"]. Then i increments to 2!
- Step 3 (i = 2 — The Skip Bug Strikes!): Because "rat" shifted into index 1 while i incremented to 2, words.get(2) inspects "ant" and completely SKIPS "rat"! "ant" is removed, leaving ["dog", "rat"].
- Step 4 (Correct Solution): Either add i-- right after words.remove(i); OR loop backward: for (int i = words.size() - 1; i >= 0; i--), which cleanly removes "ant", "rat", "bat", and "cat", leaving ["dog"].
Result:
ArrayList set() Return Value & Backward Removal Traversal
Trace the list contents and replaced value:
ArrayList<String> codes = new ArrayList<String>();
// codes initialized to ["A1", "B2", "B2", "C3"]
String old = codes.set(0, "Z9");
for (int i = codes.size() - 1; i >= 0; i--) {
if (codes.get(i).equals("B2")) { codes.remove(i); }
}
- Step 1 (Evaluate set): codes.set(0, "Z9") replaces index 0 with "Z9" and returns the previous element old = "A1".
- Step 2 (Backward removal): Iterating i from 3 down to 0 removes both adjacent "B2" elements without skipping any shifted index.
Result: old = "A1" and codes = ["Z9", "C3"]
Remove Reviews Backward and Preserve Their Order
ReviewCollector has an initialized ArrayList<ProductReview> reviewList. Each review is non-null and supplies public boolean isFlagged() and public int getHelpfulVotes(). Trace removeFlaggedReviews(2) when reviewList contains reviews A, B, C in that order: A is flagged with 1 helpful vote; B is flagged with 0; C is unflagged with 0.
public ArrayList<ProductReview> removeFlaggedReviews(int minHelpfulVotes) {
ArrayList<ProductReview> removed = new ArrayList<ProductReview>();
for (int i = reviewList.size() - 1; i >= 0; i--) {
ProductReview review = reviewList.get(i);
if (review.isFlagged() && review.getHelpfulVotes() < minHelpfulVotes) {
removed.add(0, reviewList.remove(i));
}
}
return removed;
}
- At index 2, C is not flagged, so it stays. The returned list is still empty.
- At index 1, B qualifies. Removing B leaves [A, C]; adding B at index 0 makes removed [B].
- At index 0, A qualifies. Removing A leaves [C]; inserting A at index 0 makes removed [A, B].
- Moving backward avoids skipping unexamined elements. Inserting each removed review at index 0 restores the original order in the returned list. An empty list returns an empty list.
Result: reviewList contains [C]; the method returns [A, B].