首页 > 其他 > 详细

List删除元素

时间:2020-07-08 12:38:43      阅读:52      评论:0      收藏:0      [点我收藏+]

记性不好

List<Group> groupList = groupDao.findByParentIdAndFlag(2l,0);
if (groupList.size() > 0){
Iterator it = groupList.iterator();
while(it.hasNext()){
Group group = (Group) it.next();
if (group.getId().equals(793l)) {
it.remove(); //移除该对象
}
}
}

误区:

如果将上例中的iterator.remove(); 改为list.remove(student);将会报ConcurrentModificationException异常。

这是因为:使用迭代器遍历,却使用集合的方法删除元素的结果。

再说for循环:

总体来说,不建议使用高级for循环(增强for循环)遍历删除/增加操作。

原因就是可能会报ConcurrentModificationException异常。

说可能是比较准确的,可能会报,也可能不报。

List删除元素

原文:https://www.cnblogs.com/michaelcnblogs/p/13266189.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!