You need to use the iterator directly, and remove the item via that iterator.
for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
String fruit = iterator.next();
if ("banane".equals(fruit)) {
iterator.remove();
}
System.out.println(fruit);
}
In addition to using the Iterator
directly (which I would recommend) you can also store elements that you want to remove in a different list.
List<String> toRemove = new ArrayList<String>();
for (String fruit : list) {
if ("banane".equals(fruit))
toRemove.add(fruit);
System.out.println(fruit);
}
for (String fruit : toRemove) {
list.remove(fruit);
}
Mind you, I do not recommend this, it’s just an alternative. :)
Use a for loop, and loop over the collection in a reverse order. (That means, start with the last element, and loop to the first element. By doing so, you won't get problems by the indexes that change because of removing elements from the collection.
You get the exception in the example that you post, because the list over which your iterator iterates, has changed, which means that the iterator becomes invalid.
for(Iterator<String> iter = list.iterator(); iter.hasNext(); )
{
String fruit = iter.next();
if("banana".equals(fruit))
iter.remove();
System.out.println(fruit);
}
Подобно тому, что предлагал Бомб, но менее строк кода путем повторения копии списка, но удаления из исходного списка;
List<String> temp = new ArrayList<String>(list);
for (String fruit : temp)
{
if("banane".equals(fruit))
list.remove(fruit);
System.out.println(fruit);
}
Лично я думаю, что это выглядит лучше, чем итерация с итератором.