在做项目中用到List存储数据,在里面做数据操作时候用到了删除。结果抛出ConcurrentModificationException异常。在这里把问题总结一下。

原因:

ArrayList进行foreach时所调用的迭代器(内部迭代器Itr)
/**
* An optimized version of AbstractList.Itr
*/
private class Itr implements Iterator<E> {
int cursor; // index of next element to return
int lastRet = -1; // index of last element returned; -1 if no such
// expectedModCount是Itr特有的,modCount是公共的
// expectedModCount和modCount默认是两者相等的;ArrayList进行删除修改都会更新modCount的值
// 当ArrayList通过foreach进入它的内部迭代器Itr时,expectedModCount就被赋值为modCount的值,后续ArrayList进行增加或删除,只会更新modCount,而不会同步更新expectedModCount
// 所以迭代器根据这两个值进行判断是否有并发性修改
int expectedModCount = modCount;
 
public boolean hasNext() {
return cursor != size;
}
// ArrayList通过foreach(即增强for循环)来循环是调用的是ArrayList中内部类Itr的next()
@SuppressWarnings("unchecked")
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
// ArrayList中迭代器删除方法
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
 
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
// 通过ArrayList中foreach(即通过ArrayList内部Itr的迭代器)进行删除元素
// 此时会进行赋值 expectedModCount = modCount;而不会抛出异常
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
ArrayList通过foreach迭代是调用的其内部类Itr的next方法。如果通过foreach循环,要去除某些元素,只能通过迭代器删除。因为迭代器删除后会对expectedModCount = modCount设置,不会再循环过程因为expectedModCount 和 modCount值不相等而抛出异常了。如果是通过ArrayList的删除则只会对modCount进行更新,但是ArrayList内部迭代器Itr的属性expectedModCount却没有得到更新,所以抛异常。
 
解决: List.iterator() 直接用迭代器去迭代,然后去删除元素。
 

ArrayList删除--------ConcurrentModificationException问题的更多相关文章

  1. ArrayList的ConcurrentModificationException异常和多线程下的异常

    一.ConcurrentModificationException ArrayList源码看为什么出现异常: public class ArrayList<e> extends Abstr ...

  2. ArrayList中ConcurrentModificationException

    java中两种基本的集合结构ArrayList和LinkedList底层有两种不同的存储方式实现,ArrayList为数组实现,属于顺序存储,LinkedList为链表实现,属于链式存储,在对Arra ...

  3. Android 两个ArrayList找出相同元素及单个ArrayList删除元素

    //从一个ArrayList中删除重复元素 List<String> arrayList1 = new ArrayList<String>(); arrayList1.add( ...

  4. ArrayList删除特定元素的方法

    最朴实的方法,使用下标的方式: ArrayList<String> al = new ArrayList<String>(); al.add("a"); a ...

  5. ArrayList在foreach正常迭代删除不报错的原因

    一.背景 在以前的随笔中说道过ArrayList的foreach迭代删除的问题:ArrayList迭代过程删除问题 按照以前的说法,在ArrayList中通过foreach迭代删除会抛异常:java. ...

  6. ArrayList的删除姿势你都知道了吗

    引言 前几天有个读者由于看了<ArrayList哪种遍历效率最好,你真的弄明白了吗?>问了个问题普通for循环ArrayList为什么不能删除连续重复的两个元素?其实这个描述是不正确的.正 ...

  7. jdk源码分析之ArrayList

    ArrayList关键属性分析 ArrayList采用Object数组来存储数据 /** * The array buffer into which the elements of the Array ...

  8. subList ArrayList LinkedList

    List<E> subList(int fromIndex,int toIndex) 该方法返回原有集合从fromIndex 到 toIndex之间一部分数据,组成一个新的集合,这两个集合 ...

  9. 死磕 java集合之ArrayList源码分析

    欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. 简介 ArrayList是一种以数组实现的List,与数组相比,它具有动态扩展的能力,因此也可 ...

随机推荐

  1. Java分布式锁的三种实现方案(redis)

    方案一:数据库乐观锁 乐观锁通常实现基于数据版本(version)的记录机制实现的,比如有一张红包表(t_bonus),有一个字段(left_count)记录礼物的剩余个数,用户每领取一个奖品,对应的 ...

  2. 【Git+Docker】Docker初期学习认识和安装配置详解

    Docker: 特性: 1.以应用为中心 2.自动化构建 3.版本控制 4.组件重用 5.镜像共享 6.工具生态系统 具体后续学习 优势: 1.文件系统隔离 2.进程隔离 3.网络隔离 4.资源隔离和 ...

  3. 【转】 .Net MVC4 上传大文件,并保存表单

    1前台:cshtml </pre><pre name="code" class="csharp">@model BLL.BLL.Prod ...

  4. uva-10887-枚举

    题意:集合S1和S2,把S2中的元素拼接到S1的后面去,生成新的元素,问有多少个不重复的元素 直接map.注意,不能用cin读取字符串,题目没有保证字符串中间没有空格 #include "p ...

  5. 关于php中的exec命令

    这个命令 一般是被禁用的  一些特殊需求的时刻 可以启用 比如 调用系统上的一些数据或者命令 $command ='/www/wwwroot/t.6328.net/Public/cfile/test' ...

  6. 代码:PC HTML——图片列表

    图片列表: 2016-6-12 可作为图片列表的规范性写法.这个例子只处理了单行的模式.( 一行多列 ) <link href="css/common.css" rel=&q ...

  7. 《算法》第五章部分程序 part 2

    ▶ 书中第五章部分程序,包括在加上自己补充的代码,字符串高位优先排序(计数 + 插排),(原地排序),(三路快排,与前面的三路归并排序相同) ● 计数 + 插排 package package01; ...

  8. IBeacon协议分析

    IBeacon协议分析 跳转至: 导航. 搜索 对于一个UUID是E2C56DB5-DFFB-48D2-B060-D0F5A71096E0, major是0, minjor是0的iBeacon,此时的 ...

  9. centos7 更新yum报错initscripts conflicts with centos-release-7-3.1611.el7.centos.x86_64

    1.centos7的系统的yum 更新系统报错: --> 解决依赖关系完成错误:initscripts conflicts with centos-release-7-3.1611.el7.ce ...

  10. 设计模式、j2ee 部 分、EBJ 部 分

    八. 软 件 工 程 与 设 计 模 式 1 .UML 方 面 标准建模语言 UML.用例图,静态图(包括类图.对象图和包图),行为图,交互图(顺序图,合作 图),实现图. 2 .j2ee 常 用 的 ...