Nota: sono a conoscenza del Iterator#remove()metodo.
Nel seguente esempio di codice, non capisco il motivo per cui l' List.removenel mainmetodo genera ConcurrentModificationException, ma non nel removemetodo.
public class RemoveListElementDemo {
private static final List<Integer> integerList;
static {
integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
}
public static void remove(Integer toRemove) {
for(Integer integer : integerList) {
if(integer.equals(toRemove)) {
integerList.remove(integer);
}
}
}
public static void main(String... args) {
remove(Integer.valueOf(2));
Integer toRemove = Integer.valueOf(3);
for(Integer integer : integerList) {
if(integer.equals(toRemove)) {
integerList.remove(integer);
}
}
}
}
ConcurrentModificationExceptione l'altro no.
return;nel loop.
Iterator#remove(). Perché lo fai in questo modo?