List集合遍历删除的5种实现方式

一般而言,遍历List元素有以下5种方式:

· 使用普通for循环遍历(正序删除 每次删除都使索引减1)

· 使用普通for循环遍历(倒叙删除)

· 使用增强型for循环遍历

· 使用iterator遍历

· 创建新集合,满足条件的放入新集合中

1)使用普通for循环遍历

代码如下:

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new ArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  for (int i = 0; i < list.size(); i++) {  

8.                      // index and number  

9.                      System.out.print(i + " " + list.get(i));  

10.                  if (list.get(i) % 2 == 0) {  

11.                      list.remove(list.get(i));  

12.                      System.out.print(" delete");  

13.                      i--; // 索引改变!  

14.                  }  

15.                  System.out.println();  

16.              }  

17.          }  

18.      }  

可以看到遍历删除偶数的结果是成功的,但是这种方法由于删除的时候会改变list的index索引和size大小,可能会在遍历时导致一些访问越界的问题,因此不是特别推荐。

 

2) 使用普通for循环遍历(倒叙删除)

代码如下:

1.     public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new ArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  for (int i = list.size(); i >0; i--) {  

8.                      // index and number  

9.                      System.out.print(i + " " + list.get(i));  

10.                  if (list.get(i) % 2 == 0) {  

11.                      list.remove(list.get(i));  

12.                      System.out.print(" delete");  

13.                     

14.                  }  

15.                  System.out.println();  

16.              }  

17.          }  

18.      }  

使用增强型for循环遍历

[java] view plain copy

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new ArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  for (Integer num : list) {  

8.                      // index and number  

9.                      System.out.print(num);  

10.                  if (num % 2 == 0) {  

11.                      list.remove(num);  

12.                      System.out.print(" delete");  

13.                  }  

14.                  System.out.println();  

15.              }  

16.          }  

17.      }  

可以看到删除第一个元素时是没有问题的,但删除后继续执行遍历过程的话就会抛出ConcurrentModificationException的异常。

 

使用iterator遍历

[java] view plain copy

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new ArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  Iterator<Integer> it = list.iterator();  

8.                  while (it.hasNext()) {  

9.                      // index and number  

10.                  int num = it.next();  

11.                  System.out.print(num);  

12.                  if (num % 2 == 0) {  

13.                      it.remove();  

14.                      System.out.print(" delete");  

15.                  }  

16.                  System.out.println();  

17.              }  

18.          }  

19.      }  

可以看到顺利的执行了遍历并删除的操作,因此最推荐的做法是使用iterator执行遍历删除操作。

 

以上是关于非线程安全的ArrayList,如果是线程安全的CopyOnWriteArrayList呢?

 

使用普通for循环遍历

 

[java] view plain copy

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new CopyOnWriteArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  for (int i = 0; i < list.size(); i++) {  

8.                      // index and number  

9.                      System.out.print(i + " " + list.get(i));  

10.                  if (list.get(i) % 2 == 0) {  

11.                      list.remove(list.get(i));  

12.                      System.out.print(" delete");  

13.                      i--; // 索引改变!  

14.                  }  

15.                  System.out.println();  

16.              }  

17.          }  

18.      }  


可以看到遍历删除是成功的,但是这种方法由于删除的时候会改变list的index索引和size大小,可能会在遍历时导致一些访问越界的问题,因此不是特别推荐。

 

使用增强型for循环遍历

[java] view plain copy

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new CopyOnWriteArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  for (Integer num : list) {  

8.                      // index and number  

9.                      System.out.print(num);  

10.                  if (num % 2 == 0) {  

11.                      list.remove(num);  

12.                      System.out.print(" delete");  

13.                  }  

14.                  System.out.println();  

15.              }  

16.          }  

17.      }  


可以看见与ArrayList遍历删除时情况不同,CopyOnWriteArrayList是允许使用增强型for进行循环遍历删除的。

 

使用iterator遍历

[java] view plain copy

1.          public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new CopyOnWriteArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

7.                  Iterator<Integer> it = list.iterator();  

8.                  while (it.hasNext()) {  

9.                      // index and number  

10.                  int num = it.next();  

11.                  System.out.print(num);  

12.                  if (num % 2 == 0) {  

13.                      it.remove();  

14.                      System.out.print(" delete");  

15.                  }  

16.                  System.out.println();  

17.              }  

18.          }  

19.      }  


与ArrayList不同,由于CopyOnWriteArrayList的iterator是对其List的一个“快照”,因此是不可改变的,所以无法使用iterator遍历删除。

 

综上所述,当使用ArrayList时,我们可以使用iterator实现遍历删除;而当我们使用CopyOnWriteArrayList时,我们直接使用增强型for循环遍历删除即可,此时使用iterator遍历删除反而会出现问题。

 

创建新集合,满足条件的放入新集合中

1.     public class Main {  

2.              public static void main(String[] args) throws Exception {  

3.                  List<Integer> list = new ArrayList<>();  

4.                  for (int i = 0; i < 5; i++)  

5.                      list.add(i);  

6.                  // list {0, 1, 2, 3, 4}  

   List<Integer> list 2= new ArrayList<>(); 

7.                  for (Integer num : list) {  

8.                      // index and number  

9.                      System.out.print(num);  

10.                  if (num % 2 == 0) {  

11.                    List2.add(num);

12.                  }  

13.                  System.out.println();  

14.              }  

15.          }  

16.      }  

得到的list2即为满足条件的集合



原文链接:http://blog.csdn.net/dongzhouzhou/article/details/15378433

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值