Nota: conozco el Iterator#remove()método.
En el siguiente ejemplo de código, no entiendo por qué el método List.removein mainarroja ConcurrentModificationException, pero no en el removemétodo.
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);
}
}
}
}
ConcurrentModificationExceptiony el otro no.
return;en el bucle.
Iterator#remove(). ¿Por qué lo haces de esta manera?