There are many ways to traverse and delete elements in List, and problems will arise when used improperly. Let’s take a look at the following forms of traversal deletion of elements in List:
1. Remove multiple elements that meet the criteria through an enhanced for loop
2. Remove an element that meets the criteria through an enhanced for loop
3. Delete multiple elements that meet the conditions through ordinary for deletion
4. Iterator to traverse and delete multiple elements that meet the conditions
/** * Use enhanced for loop* After deleting elements from List during the loop, ConcurrentModificationException will be reported when continuing to loop List */ public void listRemove() { List<Student> students = this.getStudents(); for (Student stu : students) { if (stu.getId() == 2) students.remove(stu); } } /** * Use an enhanced for loop to traverse and delete List, but no exception will appear if it pops up immediately after deletion*/ public void listRemoveBreak() { List<Student> students = this.getStudents(); for (Student stu : students) { if (stu.getId() == 2) { students.remove(stu); break; } } } /** * This kind of traversal may miss an element, because after deleting the element, the size of the List changes in *, and the index of the element is also changing. For example, when you loop to the second element, you delete it, * Next, you visit the third element, and actually you access the original fourth element. When the accessed element * index exceeds the size of the current List, an exception of the array cross-border will appear. Of course, this exception will not appear here, * because every time you go through it, the size of the current List is taken again. */ 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); } } } /** * You can also successfully delete and traverse by using 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)// Here you need to use the remove method of Iterator to remove the current object. If you use the remove method of List, ConcurrentModificationException will also appear stuIter.remove(); } 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(); } /** * Use enhanced for loop* After deleting elements from List during the loop, a ConcurrentModificationException will be reported when continuing to loop List */ public void listRemove() { List<Student> students = this.getStudents(); for (Student stu : students) { if (stu.getId() == 2) students.remove(stu); } } /** * Use an enhanced for loop to traverse and delete List, but no exception will appear after deletion if it pops up immediately*/ public void listRemoveBreak() { List<Student> students = this.getStudents(); for (Student stu : students) { if (stu.getId() == 2) { students.remove(stu); break; } } } } /** * This kind of unenhanced for loop does not use, and each time the size traversal of the list is re-acquisitioned, the result of the delete may be wrong. */ 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); } } /** * You can also delete and traverse successfully by using 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 + "]"; } }The above article correctly traversing and deleting elements in List (recommended) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.