Removing elements from ArrayList

I'm trying to remove certain elements from an ArrayList<String>

for(int i=0; i<myList.size(); i++)
{
    if(myList.get(i).contains("foo"))
    {
        myList.remove(i);
    }
}

This however leaves "empty spaces" in my list. I would like the list to leave out the empty elements and after iterating through it shrink to neccessary size.

Is there a smart way to do this without having to switch to a LinkedList?

Jon Skeet
people
quotationmark

This however leaves "empty spaces" in my list.

No, it doesn't. It removes entries from the list completely. Other elements are moved appropriately. What it does do with the way you've written is skip the check for the next entry... because that will have "shuffled down" to be element i, but you'll next look at element i + 1.

One simple way to avoid this is to work backwards instead:

for (int i = myList.size() - 1; i >= 0; i--) {
    if (myList.get(i).contains("foo")) {
        myList.remove(i);
    }
}

Or use an iterator as noted in other answers, of course. Both will work - the above code may be slightly more efficient if you're removing multiple entries, however, as there'll be less to shift by the time you get to the start. It's unlikely that that will be significant.

It's unfortunate that in order to use the iterator solution you have to use the iterator explicitly - you can't remove from a collection while using an enhanced for loop.

people

See more on this question at Stackoverflow