Back to Learning Hub
AP CSAJavaArrayListMay 30, 2026

AP CSA ArrayList remove(index) Loop Practice

Practice one of the most common AP CSA ArrayList traps: removing elements while indexes shift during a forward loop.

ArrayList removal questions are common in AP Computer Science A because they test two ideas at the same time: list mutation and loop control. The tricky part is not the remove method itself. The tricky part is what happens to every element after the removed index.

Why forward removal is risky

When list.remove(i) removes an element, every element after index i shifts one position to the left. If the loop then increments i, the item that shifted into index i is skipped.

ArrayList<Integer> nums = new ArrayList<>();
nums.add(2);
nums.add(4);
nums.add(6);
nums.add(7);

for (int i = 0; i < nums.size(); i++)
{
    if (nums.get(i) % 2 == 0)
    {
        nums.remove(i);
    }
}

Many students expect this code to remove all even numbers. It does not. After removing 2, the 4 shifts into index 0, but the loop moves to index 1. That means 4 is never checked.

Reliable APCSA patterns

For AP CSA, there are two reliable patterns students should recognize.

Pattern 1: traverse backward

for (int i = nums.size() - 1; i >= 0; i--)
{
    if (nums.get(i) % 2 == 0)
    {
        nums.remove(i);
    }
}

Backward traversal avoids skipped elements because removing a later element does not affect the indexes that still need to be checked.

Pattern 2: decrement the index after removal

for (int i = 0; i < nums.size(); i++)
{
    if (nums.get(i) % 2 == 0)
    {
        nums.remove(i);
        i--;
    }
}

This pattern works, but students must trace it carefully. After a removal, i-- cancels the loop update so the shifted element is checked next.

Practice check

Before running any ArrayList removal code, ask three questions: What element is removed? Which element shifts into that index? What value will the loop variable have next?

That habit turns a confusing mutation problem into a predictable tracing problem.