"
"
需求描述
在list的循环中删除匹配的节点
解决方法
for循环直接使用remove有问题,需要使用迭代器
Iterator<String> it = list.iterator();
while(it.hasNext()){
String x = it.next();
if(x.equals("del")){
it.remove();
}
}
这种方式可以正常的循环及删除。
但要注意的是,使用iterator的remove方法,如果用list的remove方法同样会报上面提到的ConcurrentModificationException错误。