Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Removing safely

โญ beginnerโณ 14 min read๐Ÿ“ Lesson 38 of 256

The length/size/charAt/get table, and the remove-while-iterating trap.

The three-way mnemonic the exam loves to test:

| | arrays | String | ArrayList | | --- | --- | --- | --- | | size | arr.length | s.length() | list.size() | | read | arr[i] | s.charAt(i) | list.get(i) | | write | arr[i] = v | (immutable) | list.set(i, v) |

Traversing an ArrayList โ€” two safe forms:

for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i));
}

for (String s : list) {          // enhanced for: read-only visits
    System.out.println(s);
}

The trap: remove shifts everything after the removed index left. Remove while walking forward and you skip the element right after each removal:

// remove all "x" from ["x", "x", "a"] โ€” WRONG
for (int i = 0; i < list.size(); i++) {
    if (list.get(i).equals("x")) {
        list.remove(i);      // i++ now skips the shifted-in element
    }
}
// result: ["x", "a"] โ€” an x survives!

Three correct fixes: walk backwards (`for (int i = list.size() - 1; i

= 0; i--)), re-check the same index after removing (i--`), or use the enhanced for only when you remove nothing. Consecutive-duplicate questions are built exactly on this trap.

โšก Now practice

Ready to Code
List practiceFilter, aggregate, mutate safely, scan in parallel.
5 challenges ยท ยท ~55 min