写代码遇到这个问题,很多博客文章都是在反复的强调理论,而没有对应的实例,所以这里从实例出发,后研究理论:

一、错误产生情况

1 、字符型

(1)添加

 public static void main(String[] args) {

        List<String> stringList = new ArrayList<String>();
stringList.add("张三毛");
stringList.add("李四");
stringList.add("王五");
stringList.add("钱二"); if (stringList!=null) {
if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())){
for (String s : stringList) {
stringList.add("赵大");
}
}
} System.out.println(stringList); }  

报错

改写为如下即可:

  public static void main(String[] args) {

         List<String> stringList = new ArrayList<String>();
stringList.add("张三毛");
stringList.add("李四");
stringList.add("王五");
stringList.add("钱二"); if (stringList != null) {
if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) { stringList.add("赵大"); Iterator<String> it = stringList.iterator();
while (it.hasNext()) {
it.next();
}
}
} System.out.println(stringList); }

打印出结果:

然后我们打印其next,就会发现其循环就是通过it.next()方法将数据添加进去的

打印:

(2)、删除

错误写法:

   private static String key = "钱二";

     public static void main(String[] args) {

         List<String> stringList = new ArrayList<String>();
stringList.add("张三毛");
stringList.add("李四");
stringList.add("王五");
stringList.add("钱二"); if (stringList != null) {
if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) { for (String s : stringList) {
if (key.equals(s)){
stringList.remove(s);
}
}
}
} System.out.println(stringList); }

报错:

改写为:

  private static String key = "钱二";

     public static void main(String[] args) {

         List<String> stringList = new ArrayList<String>();
stringList.add("张三毛");
stringList.add("李四");
stringList.add("王五");
stringList.add("钱二"); if (stringList != null) {
if (!stringList.equals(Collections.EMPTY_LIST.isEmpty())) {
Iterator<String> it = stringList.iterator(); while (it.hasNext()) {
String next = it.next(); if (key.equals(next)) {
it.remove();
} } }
} System.out.println(stringList); }

结果:

2、整形

正确添加:

 public class ConcurrentBaseApplication {

     private static String key = "钱二";

     public static void main(String[] args) {

         List<Integer> integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
integerList.add(4); Integer next=0; if (integerList != null) {
if (!integerList.equals(Collections.EMPTY_LIST.isEmpty())) {
integerList.add(5); Iterator<Integer> it = integerList.iterator(); while (it.hasNext()) {
next = it.next();
System.out.println(next);
} }
} System.out.println(integerList); } }

结果:

正确删除:

 public class ConcurrentBaseApplication {

     public static void main(String[] args) {

         List<Integer> integerList = new ArrayList<Integer>();
integerList.add(1);
integerList.add(2);
integerList.add(3);
integerList.add(4); if (integerList != null) {
if (!integerList.equals(Collections.EMPTY_LIST.isEmpty())) { Iterator<Integer> it = integerList.iterator(); while (it.hasNext()) {
Integer next = it.next();
if("2".equals(next.toString())){
it.remove();
}
}
}
} System.out.println(integerList); } }

结果:

(3) 实体类

创建实体类

Student

package com.north.big.penguin.pojo;

import java.io.Serializable;

/**
* @author liuyangos8888
*/
public class Student implements Serializable { /**
* 姓名
*/
private String name; /**
* 年龄
*/
private String age; /**
* 标识
*/
private String id; public Student() {
} public Student(String name, String age, String id) {
this.name = name;
this.age = age;
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", id='" + id + '\'' +
'}';
}
}

 正确的添加:

 public class ConcurrentBaseApplication {

     public static void main(String[] args) {

         List<Student> students = new ArrayList<Student>();

         Student student1 = new Student();
student1.setName("李雷");
student1.setAge("13");
student1.setId(UUID.randomUUID().toString()); Student student2 = new Student();
student2.setName("韩梅梅");
student2.setAge("14");
student2.setId(UUID.randomUUID().toString()); Student student3 = new Student();
student3.setName("李华");
student3.setAge("15");
student3.setId(UUID.randomUUID().toString()); students.add(student1);
students.add(student2);
students.add(student3); if (students != null) {
if (!students.equals(Collections.EMPTY_LIST.isEmpty())) { Student student4 = new Student();
student4.setName("小明");
student4.setAge("16");
student4.setId(UUID.randomUUID().toString()); Iterator<Student> it = students.iterator(); while (it.hasNext()) {
// 添加学生
Student next = it.next();
}
}
} System.out.println(students); } }

结果:

正确的删除:

 public class ConcurrentBaseApplication {

     public static void main(String[] args) {

         List<Student> students = new ArrayList<Student>();

         Student student1 = new Student();
student1.setName("李雷");
student1.setAge("13");
student1.setId(UUID.randomUUID().toString()); Student student2 = new Student();
student2.setName("韩梅梅");
student2.setAge("14");
student2.setId(UUID.randomUUID().toString()); Student student3 = new Student();
student3.setName("李华");
student3.setAge("15");
student3.setId(UUID.randomUUID().toString()); students.add(student1);
students.add(student2);
students.add(student3); if (students != null) {
if (!students.equals(Collections.EMPTY_LIST.isEmpty())) { Student student4 = new Student();
student4.setName("小明");
student4.setAge("16");
student4.setId(UUID.randomUUID().toString()); Iterator<Student> it = students.iterator(); while (it.hasNext()) {
// 添加学生
Student next = it.next(); Integer integerAge = Integer.valueOf(next.getAge()); if(integerAge>14){
it.remove();
}
}
}
} System.out.println(students); } }

结果:

结果集:

 [Student{name='李雷', age='13', id='617e914f-ed33-472d-bbbd-1a6bf5ef5901'}, Student{name='韩梅梅', age='14', id='cb804e43-4846-4fc6-84c8-9e4a6b17d7f1'}]

二、原理补充

继承关系图

根据错误我们知道是Itr出错了

根据添加class,可以看到它跟ArrayList的关系

Itr是一个内部类,还实现了Iterator

其源码

 /**
* 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
int expectedModCount = modCount; Itr() {} public boolean hasNext() {
return cursor != size;
} @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];
} public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification(); try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
} @Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
} final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}

想要调用iteror转化ArrayList就要找iterator方法

Itr参数解析

参数 含义
cursor 一个索引,代表下一个要访问的元素的索引
astRet 表示上一个访问的元素的索引
expectedModCount 表示修改次数的
modCount 表示对List的修改次数

其方法主要是hasNext()和next()两个方法,

用其判断是否还有元素未被访问,代码中

while(iter.hasNext()){

}

如果下一个访问的元素下标不等于ArrayList的大小,就表示有元素需要访问,这个很容易理解,如果下一个访问元素的下标等于ArrayList的大小,则肯定到达末尾了。

首先在next()方法中会调用checkForComodification()方法,然后根据cursor的值获取到元素,接着将cursor的值赋给lastRet,并对cursor的值进行加1操作。初始时,cursor为0,lastRet为-1,那么调用一次之后,cursor的值为1,lastRet的值为0。注意此时,modCount为0,expectedModCount也为0。
当判断当前元素的值是否为2,若为2,则调用list.remove()方法来删除该元素。

ArrayList中的remove

通过remove方法删除元素最终是调用的fastRemove()方法,在fastRemove()方法中,首先对modCount进行加1操作(因为对集合修改了一次),然后接下来就是删除元素的操作,最后将size进行减1操作,并将引用置为null以方便垃圾收集器进行回收工作。

  执行完删除操作后,继续while循环,调用hasNext方法()判断,由于此时cursor为1,而size为0,那么返回true,所以继续执行while循环,然后继续调用iterator的next()方法.

如果modCount不等于expectedModCount,则抛出ConcurrentModificationException异常。

  此时modCount为1,而expectedModCount为0,因此程序就抛出了ConcurrentModificationException异常。

java.util.ConcurrentModificationException异常;java.util.ConcurrentModificationException实战的更多相关文章

  1. java.io.InvalidClassException 异常解决, 实现Serializable接口的注意事项

    解决方案: 在类中显式指定 private static final long serialVersionUID = 42L; 类实现序列化接口, 进行序列化反序列化的时候, 抛出 java.io.I ...

  2. java集合--java.util.ConcurrentModificationException异常

    ConcurrentModificationException 异常:并发修改异常,当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常.一个线程对collection集合迭代,另一个线程对Co ...

  3. java.util.ConcurrentModificationException 异常问题详解

    环境:JDK 1.8.0_111 在Java开发过程中,使用iterator遍历集合的同时对集合进行修改就会出现java.util.ConcurrentModificationException异常, ...

  4. java.util.ConcurrentModificationException异常原因及解决方法

    在java语言中,ArrayList是一个很常用的类,在编程中经常要对ArrayList进行删除操作,在使用remove方法对ArrayList进行删除操作时,报java.util.Concurren ...

  5. java.util.ConcurrentModificationException异常分析

    Java在操作ArrayList.HashMap.TreeMap等容器类时,遇到了java.util.ConcurrentModificationException异常.以ArrayList为例,如下 ...

  6. java.util.ConcurrentModificationException 异常解决的方法及原理

    近期在修程序的bug,发现后台抛出下面异常: Exception in thread "main" java.util.ConcurrentModificationExceptio ...

  7. java foreach循环抛出异常java.util.ConcurrentModificationException

    代码如下: for (Iterator<String> iter = list.iterator(); iter.hasNext(); ) { if (Integer.parseInt(i ...

  8. java.util.ConcurrentModificationException(如何避免ConcurrentModificationException)

    java.util.ConcurrentModificationException is a very common exception when working with java collecti ...

  9. Java ConcurrentModificationException异常原因和解决方法

    Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.u ...

随机推荐

  1. [LeetCode] 674. Longest Continuous Increasing Subsequence 最长连续递增序列

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...

  2. [BZOJ1852] [MexicoOI06]最长不下降序列

    [BZOJ1852] [MexicoOI06]最长不下降序列 额我也不知道是不是水过去的...和网上的另一篇题解对拍过了,但是拍不出来... 经过和神仙的讨论基本可以确定是对的了 考虑如下贪心 (我将 ...

  3. torch_07_卷积神经网络案例分析

    1. LeNet(1998) """ note: LeNet: 输入体:32*32*1 卷积核:5*5 步长:1 填充:无 池化:2*2 代码旁边的注释:卷积或者池化后的 ...

  4. oracle数据库安装过程中的疑惑—该记录是本人以前写在微博上的文章

    转行IT初学者关于oracle数据库整理第一次安装数据库的时候都是按照操作步骤一步一步进行安装,并没有对操作步骤产生过怀疑或者为什么要这么进行操作?2017年12月8日再次阅读安装操作说明书的时候有了 ...

  5. UML类图记忆口诀

    UML类图在设计模式书籍中用的比较多,经常忘记,口诀挺重要的,比如我们从小到大,除了乘法口诀.元素周期表等口诀形式的知识,其它的知识都基本忘记了, 所以编写口诀如下 1.三级石 2.见关一 3.零足迹 ...

  6. SSM基本配置详解

    需要查看SSM基本依赖和完整配置文件的到:SSM基本配置及依赖 示例项目:SSMDemo 1 Spring IOC容器配置 1.1 applicationContext.xml 1.1.1 配置数据源 ...

  7. 如何将云上的Linux文件自动备份到本地服务器

    需求场景: 将云上一台Linux服务器文件备份到本地服务器,一周一备即可. 面对这样一个需求,我们可能面临下列几个问题, 备份方式:是云服务器推文件到本地服务器写入,还是本地服务器从云服务器拉文件?这 ...

  8. 【leetcode-198】打家劫舍

    你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个代表每 ...

  9. [原创]SpringSecurity控制授权(鉴权)功能介绍

    1.spring security 过滤器链 ​ spring security中的除了用户登录校验相关的过滤器,最后还包含了鉴权功能的过滤器,还有匿名资源访问的过滤器链,相关的图解如下: 2.控制授 ...

  10. logstash 对配置文件conf敏感信息,密码等加密

    logstash的配置文件conf经常会涉及敏感信息,比如ES,mysql的账户密码等,以下使用logstash导入mysql为例子,加密隐藏mysql的密码. 在向keystore中添加key及其s ...