Data collections
Traverse arrays and ArrayList
Choose a collection, handle its bounds, and remove elements without skipping them.
Examples use Java. Read and trace them here; run them in a Java environment. The studio executes pseudocode.
Track size and valid positions
An array has a fixed length. An ArrayList can change its size. Both use zero-based positions: an array's last valid index is length - 1 and an ArrayList's is size() - 1. A two-dimensional array is indexed by row and then column.
Removing an ArrayList element shifts later elements left. When removing through a forward index, the next element can move into the position you just visited. A backwards traversal avoids skipping it.
import java.util.ArrayList;
public class Demo {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(40); scores.add(35); scores.add(70);
for (int i = scores.size() - 1; i >= 0; i--) {
if (scores.get(i) < 50) { scores.remove(i); }
}
System.out.println(scores);
}
}Test the traversal
The example prints [70]. Test an empty collection, all matching elements, and consecutive matching elements. For two-dimensional arrays, use each row's length rather than assuming all rows have the same number of columns.
When reading a dataset from a text file, separate parsing from analysis. Check missing or malformed values before adding them to a collection. A successful traversal cannot repair incorrect assumptions about the input data.
Check yourself
Why would a forward removal loop miss two adjacent low scores?Show answer
After the first removal the second score shifts left, then the loop increments past its new index.
References used to build these lessons
We used these official documents as references when building this course's lessons. They are the sources for the syllabus structure and course requirements.
College Board · Effective fall 2025