在foreach循环中使用remove报ConcurrentModificationException异常原因

在foreach循环中使用remove报ConcurrentModificationException异常原因

我的代码具体是这样的

   int dindex=0;
                    int did=getInt("请输入需要删除的学生学号:");
                    for (Student student:list) {
                        if(student.id==did){
                            list.remove(student);
                        }
                    }

这样会导致remove后,导致list在循环中下标和实际已经被修改后的下标不一致

我自己的解决方案是:

int dindex=-1;
                    int did=getInt("请输入需要删除的学生学号:");
                    for (Student student:list) {
                        if(student.id==did){
                            dindex=list.indexOf(student);
                        }
                    }
                    if(dindex!=-1)
                        list.remove(dindex);
                    else
                        System.out.println("没有此学生");

记录下标 不改变list本身 等foreach结束后,再删除

原文地址:https://www.cnblogs.com/7-30-onlyone/p/11296318.html

时间: 2024-07-29 16:11:30

在foreach循环中使用remove报ConcurrentModificationException异常原因的相关文章

Java中List.remove报UnsupportedOperationException异常

今天项目中有个需求场景: A和B都是List,而B是A的子集,现在想求A和B的差集. 想到了List中提供的removeAll()方法可以求得差集,但是结果确报了UnsupportedOperationException异常. 仔细分析了下,List A我是通过数组经过Arrays.asList()转化成List,但是它是继承AbstractList的子类,而AbstractList是不支持removeAll()和remove()操作的. 如果要执行remove()和removeAll()操作,

C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响)

C#不允许在foreach循环中改变数组或集合中元素的值(注:成员的值不受影响),如以下代码将无法通过编译. foreach (int x in myArray) { x++; //错误代码,因为改变了元素的值 Console.WriteLine(x); } 如果要让自定义的数据类型支持foreach循环,则该类型必须实现IEnumerable<T>接口,且存在对应此列表的IEnumerator<T>实现. 实际上,在.Net的底层(IL语言层面)而言, foreach (var

C#在foreach循环中修改字典等集合出错的处理

C#在foreach循环中修改字典等集合出错:System.InvalidOperationException: Collection was modified; enumeration operation may not execute.这是因为在foreach中不允许修改集合,可通过如下方式修改dictPublish的值,如: Dictionary<string, string> _dict = new Dictionary<string, string>(dictPublis

「译」forEach循环中你不知道的3件事

前言 本文925字,阅读大约需要7分钟. 总括: forEach循环中你不知道的3件事. 原文地址:3 things you didn't know about the forEach loop in JS 公众号:「前端进阶学习」,回复「666」,获取一揽子前端技术书籍 自弃者扶不起,自强者击不倒. 正文 你觉得你真的学会用forEach了么? 这是我之前对forEach循环的理解:就是一个普通语义化之后的for循环,可以被break,continue,return. 这篇文章将向你展示for

Java中List.remove报错UnsupportedOperationException

Java中List.remove(removeRange,clear类似) 报出 UnsupportedOperationException 的错误.原来该List是一个AbstractList,不支持增删改操作. 一般情况下我们会使用 LinkedList 和 ArrayList ,什么情况下出现 AbstractList 呢?通过 ArrayList.asList() 函数得到的 List 就是 AbstractList.该AbstractList只是简单地在已有的元素数组上套了一层List

12 在Foreach循环中如何获得当前迭代的索引

static void ForEachIndex() { int curIndex = 0; Dictionary<string, string> diction = new Dictionary<string,string>(); int counter = 0; diction.Add("One", "One"); diction.Add("Two", "Two"); diction.Add(&qu

Java并发编程:Java ConcurrentModificationException异常原因和解决方法

Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常.下面我们就来讨论以下这个异常出现的原因以及解决办法. 以下是本文目录大纲: 一.ConcurrentModificationException异常出现的原因 二.在单线程环境下的解决办法 三.在多线程环境下的解决方法 若有不

【转】Java ConcurrentModificationException异常原因和解决方法

原文网址:http://www.cnblogs.com/dolphin0520/p/3933551.html Java ConcurrentModificationException异常原因和解决方法 在前面一篇文章中提到,对Vector.ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常.下面我们就来讨论以下这个异常出现的原因以及解决办法. 以下是本文目录大纲: 一.ConcurrentModific

for循环中使用remove方法。

List<String> list =new ArrayList<String>(); list.add("boss"); list.add("good"); list.add("No"); list.add("Fine"); System.out.println(list);//[boss, good, No, Fine] Collections.sort(list); System.out.prin