遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题。下面主要看看以下几种遍历删除List中元素的形式:
1.通过增强的for循环删除符合条件的多个元素
2.通过增强的for循环删除符合条件的一个元素
3.通过普通的for删除删除符合条件的多个元素
4.通过Iterator进行遍历删除符合条件的多个元素
- /**
- * 使用增强的for循环
- * 在循环过程中从List中删除元素以后,继续循环List时会报ConcurrentModificationException
- */
- public void listRemove() {
- List<Student> students = this.getStudents();
- for (Student stu : students) {
- if (stu.getId() == 2)
- students.remove(stu);
- }
- }
- /**
- * 像这种使用增强的for循环对List进行遍历删除,但删除之后马上就跳出的也不会出现异常
- */
- public void listRemoveBreak() {
- List<Student> students = this.getStudents();
- for (Student stu : students) {
- if (stu.getId() == 2) {
- students.remove(stu);
- break;
- }
- }
- }
- /**
- * 这种遍历有可能会遗漏某个元素,因为删除元素后List的size在
- * 变化,元素的索引也在变化,比如你循环到第2个元素的时候你把它删了,
- * 接下来你去访问第3个元素,实际上访问到的是原先的第4个元素。当访问的元素
- * 索引超过了当前的List的size后还会出现数组越界的异常,当然这里不会出现这种异常,
- * 因为这里每遍历一次都重新拿了一次当前List的size。
- */
- public void listRemove2() {
- List<Student> students = this.getStudents();
- for (int i=0; i<students.size(); i++) {
- if (students.get(i).getId()%3 == 0) {
- Student student = students.get(i);
- students.remove(student);
- }
- }
- }
- /**
- * 使用Iterator的方式也可以顺利删除和遍历
- */
- public void iteratorRemove() {
- List<Student> students = this.getStudents();
- System.out.println(students);
- Iterator<Student> stuIter = students.iterator();
- while (stuIter.hasNext()) {
- Student student = stuIter.next();
- if (student.getId() % 2 == 0)
- stuIter.remove();//这里要使用Iterator的remove方法移除当前对象,如果使用List的remove方法,则同样会出现ConcurrentModificationException
- }
- System.out.println(students);
- }
附完整代码如下:
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class ListRemove {
- public static void main(String args[]) {
- ListRemove lr = new ListRemove();
- lr.listRemove();
- lr.listRemoveBreak();
- // lr.listRemove2();
- // lr.iteratorRemove();
- }
- /**
- * 使用增强的for循环
- * 在循环过程中从List中删除元素以后,继续循环List时会报ConcurrentModificationException
- */
- public void listRemove() {
- List<Student> students = this.getStudents();
- for (Student stu : students) {
- if (stu.getId() == 2)
- students.remove(stu);
- }
- }
- /**
- * 像这种使用增强的for循环对List进行遍历删除,但删除之后马上就跳出的也不会出现异常
- */
- public void listRemoveBreak() {
- List<Student> students = this.getStudents();
- for (Student stu : students) {
- if (stu.getId() == 2) {
- students.remove(stu);
- break;
- }
- }
- }
- /**
- * 这种不使用增强的for循环,每次重新获取list的size遍历的情况运行时不会报错,但是可能删除的结果是错的。
- */
- public void listRemove2() {
- List<Student> students = this.getStudents();
- for (int i=0; i<students.size(); i++) {
- if (students.get(i).getId()%2 == 0)
- students.remove(i);
- }
- }
- /**
- * 使用Iterator的方式也可以顺利删除和遍历
- */
- public void iteratorRemove() {
- List<Student> students = this.getStudents();
- System.out.println(students);
- Iterator<Student> stuIter = students.iterator();
- while (stuIter.hasNext()) {
- Student student = stuIter.next();
- if (student.getId() % 2 == 0)
- stuIter.remove();
- }
- System.out.println(students);
- }
- private List<Student> getStudents() {
- List<Student> students = new ArrayList<Student>() {
- {
- int i = 0;
- while (i++ < 10) {
- Student student = new Student(i, "201200" + i, "name_" + i);
- this.add(student);
- }
- }
- };
- return students;
- }
- }
- public class Student {
- private int id;
- private String stuNo;
- private String name;
- public Student() {
- }
- public Student(int id, String stuNo, String name) {
- this.id = id;
- this.stuNo = stuNo;
- this.name = name;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getStuNo() {
- return stuNo;
- }
- public void setStuNo(String stuNo) {
- this.stuNo = stuNo;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "Student [id=" + id + ", name=" + name + ", stuNo=" + stuNo
- + "]";
- }
- }
- 转载:http://haohaoxuexi.iteye.com/blog/1523785
相关推荐
Iterator遍历中 ConcurrentModificationException异常
NULL 博文链接:https://chenlinbo.iteye.com/blog/832335
java.util.ConcurrentModificationException 异常问题详解1
在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util.ConcurrentModificationException at java.util.HashMap$...
主要介绍了出现java.util.ConcurrentModificationException 问题及解决办法的相关资料,需要的朋友可以参考下
Map在遍历时候通常 现获得其键值的集合Set,然后用迭代器Iterator来对Map进行遍历。
axis1.4补丁包,解决jdk1.8高并发报ConcurrentModificationException问题,该jar包重新编译jar包的一个class文件,线上环境通过
今天小编就为大家分享一篇关于Java源码解析ArrayList及ConcurrentModificationException,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Spring数据mongodb测试 在Collections.synchronizedList或Collections.synchronizedSet上测试spring数据mongodb ConcurrentModificationException
axis1.4 spring3.0 集成 实现 web service 服务端, axis1.4 客户端认证,授权,访问日志记录,集成spring 解决 PHP 调用web service 无法认证,和解析soap 模板
这里面包含了大部分的软件测试的专业术语,希望对你有用
fastJson的全部资料,包括源码、开发需要用到的jar包和html格式的文档。
这时候我们就要用到Iterator,经常写程序的朋友应该都知道,在Iterator遍历的过程中,是不能够修改集合数据的,否则就会抛出ConcurrentModificationException。 因为ConcurrentModificationException的存在,就把...
java.util.ConcurrentModificationException: mutation occurred during iteration [error] scala.collection.mutable.MutationTracker$.checkMutations(MutationTracker.scala:43) [error] scala.collection....
那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。 fail-fast 机制是java集合(Collection)中的一种错误机制。当多个线程对同一个集合的内容进行操作时,就可能会产生fail-...
迭代时被修改抛出ConcurrentModificationException异常 迭代时集合被修改不抛出异常 使用原集合遍历集合元素 使用原集合的副本遍历集合元素 迭代器不要求额外的内存 迭代器需要额外的内存克隆集合对象 示例:...
Java中的For each实际上使用的是iterator进行处理的。而iterator是不允许集合在...而我在for each时,从集合中删除了一个元素,这导致了iterator抛出了ConcurrentModificationException,下面来看看到底怎么回事。
Axis1.4快速发布服务以及客服端详解(资源中的axis是axis1.4自带的,lib也是它自带的)