List遍历删除

遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题。下面主要看看以下几种遍历删除List中元素的形式:

1.通过增强的for循环删除符合条件的多个元素

2.通过增强的for循环删除符合条件的一个元素

3.通过普通的for删除删除符合条件的多个元素

4.通过Iterator进行遍历删除符合条件的多个元素

 

public void listRemove(){
    List<Student> students = this.getStudents();
    List<Student> remove=new ArrayList<Student>();
    for(int i=0;i<students.size();i++) {
           if(students.get(i).getId()==2){
               remove.add(students.get(i));
           } 
    }
    students.removeAll(remove);
}


Java代码   收藏代码
  1. /** 
  2.  * 使用增强的for循环 
  3.  * 在循环过程中从List中删除非基本数据类型以后,继续循环List时会报ConcurrentModificationException 
  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.     }  
  11. }  

 

 

Java代码   收藏代码
  1. /** 
  2.  * 像这种使用增强的for循环对List进行遍历删除,但删除之后马上就跳出的也不会出现异常 
  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. }  

 

 

Java代码   收藏代码
  1. /** 
  2.  * 这种不使用增强的for循环的也可以正常删除和遍历, 
  3.  * 这里所谓的正常是指它不会报异常,但是删除后得到的 
  4.  * 数据不一定是正确的,这主要是因为删除元素后,被删除元素后 
  5.  * 的元素索引发生了变化。假设被遍历list中共有10个元素,当 
  6.  * 删除了第3个元素后,第4个元素就变成了第3个元素了,第5个就变成 
  7.  * 了第4个了,但是程序下一步循环到的索引是第4个, 
  8.  * 这时候取到的就是原本的第5个元素了。 
  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. }  

 

 

Java代码   收藏代码
  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. }  

 

 

附完整代码如下:

 

Java代码   收藏代码
  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4.   
  5. public class ListRemove {  
  6.   
  7.     public static void main(String args[]) {  
  8.         ListRemove lr = new ListRemove();  
  9.         lr.listRemove();  
  10.         lr.listRemoveBreak();  
  11. //      lr.listRemove2();  
  12. //      lr.iteratorRemove();  
  13.     }  
  14.   
  15.     /** 
  16.      * 使用增强的for循环 
  17.      * 在循环过程中从List中删除非基本数据类型以后,继续循环List时会报ConcurrentModificationException 
  18.      */  
  19.     public void listRemove() {  
  20.         List<Student> students = this.getStudents();  
  21.         for (Student stu : students) {  
  22.             if (stu.getId() == 2)   
  23.                 students.remove(stu);  
  24.         }  
  25.     }  
  26.       
  27.     /** 
  28.      * 像这种使用增强的for循环对List进行遍历删除,但删除之后马上就跳出的也不会出现异常 
  29.      */  
  30.     public void listRemoveBreak() {  
  31.         List<Student> students = this.getStudents();  
  32.         for (Student stu : students) {  
  33.             if (stu.getId() == 2) {  
  34.                 students.remove(stu);  
  35.                 break;  
  36.             }  
  37.         }  
  38.     }  
  39.       
  40.     /** 
  41.      * 这种不使用增强的for循环的就可以正常删除和遍历 
  42.      */  
  43.     public void listRemove2() {  
  44.         List<Student> students = this.getStudents();  
  45.         for (int i=0; i<students.size(); i++) {  
  46.             if (students.get(i).getId()%2 == 0)  
  47.                 students.remove(i);  
  48.         }  
  49.     }  
  50.       
  51.     /** 
  52.      * 使用Iterator的方式也可以顺利删除和遍历 
  53.      */  
  54.     public void iteratorRemove() {  
  55.         List<Student> students = this.getStudents();  
  56.         System.out.println(students);  
  57.         Iterator<Student> stuIter = students.iterator();  
  58.         while (stuIter.hasNext()) {  
  59.             Student student = stuIter.next();  
  60.             if (student.getId() % 2 == 0)  
  61.                 stuIter.remove();  
  62.         }  
  63.         System.out.println(students);  
  64.     }  
  65.       
  66.     private List<Student> getStudents() {  
  67.         List<Student> students = new ArrayList<Student>() {  
  68.             {  
  69.                 int i = 0;  
  70.                 while (i++ < 10) {  
  71.                     Student student = new Student(i, "201200" + i, "name_" + i);  
  72.                     this.add(student);  
  73.                 }  
  74.             }  
  75.         };  
  76.         return students;  
  77.     }  
  78. }  

 

 

Java代码   收藏代码
  1. public class Student {  
  2.   
  3.     private int id;  
  4.     private String stuNo;  
  5.     private String name;  
  6.       
  7.     public Student() {  
  8.           
  9.     }  
  10.       
  11.     public Student(int id, String stuNo, String name) {  
  12.         this.id = id;  
  13.         this.stuNo = stuNo;  
  14.         this.name = name;  
  15.     }  
  16.   
  17.     public int getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(int id) {  
  22.         this.id = id;  
  23.     }  
  24.   
  25.     public String getStuNo() {  
  26.         return stuNo;  
  27.     }  
  28.   
  29.     public void setStuNo(String stuNo) {  
  30.         this.stuNo = stuNo;  
  31.     }  
  32.   
  33.     public String getName() {  
  34.         return name;  
  35.     }  
  36.   
  37.     public void setName(String name) {  
  38.         this.name = name;  
  39.     }  
  40.   
  41.     @Override  
  42.     public String toString() {  
  43.         return "Student [id=" + id + ", name=" + name + ", stuNo=" + stuNo  
  44.                 + "]";  
  45.     }  
  46.       
  47. }  
4 
1 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值