集合遍历过程iterator, 添加删除元素报异常

list  set  遍历过程中添加或者删除元素,报异常。

使用iterator 也会报异常 ConcurrentModificationException

remove只能用迭代器的remove,而不能用集合的remove方法,iterator的remove会维护索引的一致性

iterator it = list.iterator();

while(it.hasnext(0){

  obj =   it.next();

  it.remove();

}

用java.util.concurrent中的类代替相应的集合类

原文地址:https://www.cnblogs.com/wuer888/p/7341826.html