mashiguang

小马快跑

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  20 随笔 :: 0 文章 :: 60 评论 :: 0 Trackbacks
在foreach调用remove可能会引发ConcurrentModificationException。
如:
for(Object obj : list){
    .remove(obj);
}

最好像下面这样写:
for (Iterator it = list.iterator(); it.hasNext();) {
......
it.remove();
}

(添加20160420)
如果foreach的同时需要add element,就不能使用iterator了,最好重新启用一个新的list暂存新的集合元素。
posted on 2010-12-08 09:47 mashiguang 阅读(406) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: