By LearnAllStudents Editorial Team. Source check dates are listed below.
Official resources
Trace what shifts after removal
If [9, 1, 2, 8] loses its element at index 1, it becomes [9, 2, 8]. The 2 moves into index 1. A forward loop that immediately increments its index skips that 2.
Remove in reverse index order
static void removeBelow(ArrayList<Integer> values, int cutoff) {
for (int i = values.size() - 1; i >= 0; i--) {
if (values.get(i) < cutoff) {
values.remove(i);
}
}
}Deleting an item shifts items to its right. A backward loop has inspected those items already, so the next smaller index still points to the next uninspected value.
Scroll the table to see all columns. Keyboard: focus the table area and use the arrow keys.
| Input | Cutoff | Expected result |
|---|---|---|
| [9, 1, 2, 8] | 5 | [9, 8] |
| [5, 4, 5] | 5 | [5, 5] |
| [] | 5 | [] |
Read the mutation requirement
This method edits the input list. If a prompt asks you to return a new list, create a result and append qualifying values while iterating forward. Reverse iteration with appends would reverse their relative order.
Try it yourself
For [3, 6, 1, 6, 0] and cutoff 3, trace the indexes inspected by the method. Which values remain? What changes if you replace < with <=?
Check your reasoning
The method visits indexes 4, 3, 2, 1 and 0, reading 0, 6, 1, 6 and 3. The first condition leaves [3, 6, 6]. Changing the comparison removes 3 as well, leaving [6, 6].
How a tutor would approach this
Test neighbouring deletions. Try an empty list, a list with all values below the cutoff, and a list with two consecutive removals. Include a value equal to the cutoff to check the comparison.
Frequently asked questions
Why begin at size() - 1?
List indexes start at zero, so size() itself is outside the list.
Can a forward loop work?
Yes, if it advances only when no removal occurs or otherwise handles the shift.
What does values.remove(i) do here?
With i an int variable, it calls the index-based overload.
Should I always edit the original list?
Read whether the prompt asks for mutation, a count or a newly constructed list.
Official sources
- College Board AP Computer Science A exam and Java Quick Reference (checked 2026-11-26)
- College Board AP Computer Science A sample questions (checked 2026-11-26)
Related pages
Try a free 30-minute demo
Work through a question with a tutor. No payment needed.
Request a free 30-minute demo