This little annoyance caused me to lose an hour of sleep, and I don't understand why.
I have an ArrayList array which I want to iterate over and conditionally remove items. This was my first attempt:
for (int i = 0; i < array.size(); i++) {
if (array.get(i) == conditionMet) array.remove(i);
}
And that did not work. The following did:
for (Iterator<T> i = array.iterator(); i.hasNext();) {
if (i.next() == conditionMet) i.remove();
}
Why?