关于List比较好玩的操作

       作为Java大家庭中的集合类框架,List应该是平时开发中最常用的,可能有这种需求,当集合中的某些元素符合一定条件时,想要删除这个元素。如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       // for循环优化写法,只获取一次长度
       for(int i = 0, size = intList.size(); i < size; i++) {
           Integer value = intList.get(i);
           // 符合条件,删除元素
           if(value == 3 || value == 5) {
              intList.remove(i);
           }
       }
       System.out.println(intList);
    }
}

       执行后,会抛出IndexOutOfBoundsException,因为集合中存在符合条件的元素,删除后,集合长度动态改变,由于长度只获取一次,发生越界,所以,去掉for循环优化,如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       for(int i = 0; i < intList.size(); i++) {
           Integer value = intList.get(i);
           // 符合条件,删除元素
           if(value == 3 || value == 5) {
              intList.remove(i);
           }
       }
       System.out.println(intList);
    }
}

       输出:[1, 2, 5, 6],漏掉了5这个元素,当i=2的时候,值为3,删除后,后面的元素往前补一位,这时i=3的时候,值为6,跳过了5,这样也不行,随后想到了用for循环增强,不显示的操作下标,直接操作对象,如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       for(Integer value : intList) {
           // 符合条件,删除元素
           if(value == 3 || value == 5) {
              intList.remove(value);
           }
       }
       System.out.println(intList);
    }
}

       执行后,会抛出ConcurrentModificationException,字面意思是并发修改异常。异常跟踪信息如下:

Exception inthread "main" java.util.ConcurrentModificationException

         atjava.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)

         at java.util.AbstractList$Itr.next(AbstractList.java:420)

         at ListTest.main(ListTest.java:13)

       可以大概看出是执行到AbstractList中内部类Itr的checkForComodification方法抛出的异常,至于为什么出现异常,这里可以大概解释一下。集合遍历是使用Iterator, Iterator是工作在一个独立的线程中,并且拥有一个互斥锁。Iterator 被创建之后会建立一个指向原来对象的单链索引表,当原来的对象数量发生变化时,这个索引表的内容不会同步改变,所以当索引指针往后移动的时候就找不到要迭代的对象,所以按照 fail-fast原则 Iterator 会马上抛出java.util.ConcurrentModificationException 异常。所以 Iterator 在工作的时候是不允许被迭代的对象被改变的。

       而要解决这个问题,可以使用Iterator的remove方法,该方法会删除当前迭代对象的同时,维护索引的一致性。如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       Iterator<Integer> it = intList.iterator();
       while(it.hasNext()) {
           Integer value = it.next();
           if(value == 3 || value == 5) {
              it.remove();
           }
       }
       System.out.println(intList);
    }
}

       输出正确结果:[1, 2, 6]。

       不使用迭代器的解决方案就是,自己维护索引,删除一个元素后,索引-1,如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       for(int i = 0; i < intList.size(); i++) {
           Integer value = intList.get(i);
           if(value == 3 || value == 5) {
              intList.remove(i);
              i--;
           }
       }
        System.out.println(intList);
    }
}

       输出正确结果:[1, 2, 6]。

       还有种取巧的方式是从最后一个元素开始遍历,符合条件的删除,如:

public class ListTest {
    public static void main(String[] args) {
       List<Integer> intList = new ArrayList<Integer>();
	   Collections.addAll(intList, 1, 2, 3, 5, 6);
       for(int i = intList.size() - 1; i >= 0; i--) {
           Integer value = intList.get(i);
           if(value == 3 || value == 5) {
              intList.remove(i);
           }
       }
        System.out.println(intList);
    }
}

       输出正确结果:[1, 2, 6]。

       最后,Java集合类框架真是大大方便了开发,不用自己去维护数组,随时担心着越界等问题。当然List的实现类对插入、删除的效率不太一样,这取决于其实现的数据结构,是选择删除,还是选择新建个集合,这里就不做讨论了。

       本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/9347357

  • 13
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值