How to properly traverse to delete elements in a List, would you?

There are many ways to traverse and delete elements in a List, which can cause problems when used incorrectly. Let's take a look at the following forms of traversing and deleting elements in a List:

1. Delete multiple elements that meet the conditions through an enhanced for loop

2. Remove an element that meets the conditions through an enhanced for loop

3. Delete multiple elements that meet the conditions through ordinary for deletion

4. Traverse through Iterator to delete multiple elements that meet the conditions

 

  1. /**  
  2.  * Use an enhanced for loop  
  3.  * After the non-basic data type is deleted from the List during the loop, ConcurrentModificationException will be reported when continuing to loop the List  
  4.  */    
  5. public void listRemove() {    
  6.     List<Student> students = this.getStudents();    
  7.     for (Student stu : students) {    
  8.         if (stu.getId() == 2)     
  9.             students.remove(stu);    
  10.     }    

 

 

  1. /**  
  2.  * Use the enhanced for loop to traverse and delete the List like this, but there will be no exception if it jumps out immediately after deletion  
  3.  */    
  4. public void listRemoveBreak() {    
  5.     List<Student> students = this.getStudents();    
  6.     for (Student stu : students) {    
  7.         if (stu.getId() == 2) {    
  8.             students.remove(stu);    
  9.             break;    
  10.         }    
  11.     }    
  12. }  

 

 

 

  1. /**  
  2.  * This kind of deletion and traversal can also be done normally without using the enhanced for loop,  
  3.  * The so-called normal here means that it will not report an exception, but the result obtained after deletion  
  4.  * The data is not necessarily correct, this is mainly because after the element is deleted, after the element is deleted  
  5.  The element index of * has changed. Assuming that there are 10 elements in the traversed list, when  
  6.  * After deleting the 3rd element, the 4th element becomes the 3rd element, and the 5th element becomes  
  7.  * The fourth one, but the index to which the program loops next is the fourth one,  
  8.  * At this time, the original 5th element is obtained.  
  9.  */    
  10. public void listRemove2() {    
  11.     List<Student> students = this.getStudents();    
  12.     for (int i=0; i<students.size(); i++) {    
  13.         if (students.get(i).getId()%3 == 0) {    
  14.             Student student = students.get(i);    
  15.             students.remove(student);    
  16.         }    
  17.     }    
  18. }    

 

 

  1. /**  
  2.  * 使用Iterator的方式可以顺利删除和遍历  
  3.  */    
  4. public void iteratorRemove() {    
  5.     List<Student> students = this.getStudents();    
  6.     System.out.println(students);    
  7.     Iterator<Student> stuIter = students.iterator();    
  8.     while (stuIter.hasNext()) {    
  9.         Student student = stuIter.next();    
  10.         if (student.getId() % 2 == 0)    
  11.             stuIter.remove();//这里要使用Iterator的remove方法移除当前对象,如果使用List的remove方法,则同样会出现ConcurrentModificationException    
  12.     }    
  13.     System.out.println(students);    
  14. }    

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326873288&siteId=291194637