ArrayList and Safe Removal
AP Computer Science A · Unit 4 — Data Collections
The six methods the exam tests
An ArrayList<E> grows and shrinks at run time. The type parameter must be an object type, so a list of integers is declared ArrayList<Integer> list = new ArrayList<Integer>(); — never ArrayList<int>.
int size() number of elements E get(int i) element at index i E set(int i, E x) replaces element i, RETURNS the old one boolean add(E x) appends at the end, returns true void add(int i, E x) inserts at i, shifts everything right E remove(int i) removes element i, RETURNS it, shifts left
Two of these are quietly important. set and remove return the element that used to be there, which lets a question hide a value in an expression such as System.out.println(list.remove(0));.
Autoboxing
list.add(5) works on an ArrayList<Integer> because Java boxes the int into an Integer. Going the other way, int n = list.get(0); unboxes automatically.
i++ sends it straight to position 2. The element that just moved down is never examined. The consequence is very characteristic: in a sequence where two values to be removed are next to each other, the second one survives, while in a sequence with no adjacent values the program runs correctly — so this error slips through most hastily written test cases.Worked example: removing correctly, two ways
Given an ArrayList<Integer> list. Remove every element equal to 4. Write two correct solutions and point out how they differ from the incorrect version.
Solution
The incorrect version — traverses forward and still increments i after removing:
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 4) { list.remove(i); } // skips one
}Way 1 — traverse forward, increment the index only when NOT removing:
int i = 0;
while (i < list.size()) {
if (list.get(i) == 4) {
list.remove(i); // do not increment i
} else {
i++;
}
}Way 2 — traverse backward from the end to the start:
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i) == 4) { list.remove(i); }
}Way 2 is safe because the shifting only affects positions after the one just removed, and the backward loop has already passed those positions. In the exam room, Way 2 is shorter and has fewer places to go wrong.
With [4, 4, 7, 4, 9]: the incorrect version gives [4, 7, 9], and both correct ways give [7, 9].
Common mistake
The costliest trap of the chapter: removing elements while traversing forward. It throws no exception and reports no error; it quietly skips exactly those elements that sit next to each other. The way to prevent it is two mechanical steps, and that is all: one, whenever the loop body containsremove, do not use for-each (it throws ConcurrentModificationException) and do not use the default forward for; two, choose one of the two ways you have learned — traverse backward, or a while that increments the index only in the else branch. Trying the case [4, 4] yourself before submitting will catch it immediately.Key takeaway
set and remove return the old value; add(i, x) and remove(i) shift elements and change size(). To remove while traversing, traverse backward, or increment the index only when not removing.