集合遍历过程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中的类代替相应的集合类

 

posted @ 2017-08-10 22:36  上台阶  阅读(690)  评论(0编辑  收藏  举报