我想从循环内的ArrayList中删除元素。从循环内的列表中删除元素
这是好的。
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
for(Integer i: list){
if(i == 2)
list.remove(i);
}
但是,这是不是和抛出ConcurrentModificationException。
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3));
for(Integer i: list){
list.remove(i);
}
我不明白为什么。
我刚添加了另一个元素,它也不行(抛出concurrentMOdificationException)。
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4));
System.out.println(list);
for (Integer i : list) {
if (i == 2)
list.remove(i);
}
可能因为元素'2'不在'list'中。 – rgettman
http://stackoverflow.com/q/8189466/738746 - 相关?再添加一个元素,它也会抛出CME。 –
@rgettman不,它在列表中。 – Ryan