I'm looping through an ArrayList and removing everything that matches a condition, but some matching elements survive. It seems to skip every other one.
Accepted answer
When you remove index `i`, every later element shifts left by one — but your loop still does `i++`, so the element that slid into position `i` never gets checked.
Two standard fixes:
```java
// 1. iterate backwards
for (int i = list.size() - 1; i >= 0; i--) {
if (test(list.get(i))) list.remove(i);
}
// 2. don't advance when you remove
int i = 0;
while (i < list.size()) {
if (test(list.get(i))) list.remove(i);
else i++;
}
```
Backwards iteration is usually cleanest, because removing at the end never shifts anything you still have to visit.