list集合需要深入了解的问题
来源:互联网 发布:淘宝开两家店 编辑:程序博客网 时间:2024/06/06 19:57
转载自:http://blog.csdn.net/ghsau/article/category/793599/1
作为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的实现类对插入、删除的效率不太一样,这取决于其实现的数据结构,是选择删除,还是选择新建个集合,这里就不做讨论了。
- list集合需要深入了解的问题
- 深入Collection集合的子接口List
- 将list集合的数据转换为json字符串需要注意的问题
- list集合的contains问题
- list集合深入理解
- Map集合与List集合的使用的深入总结
- 学习J2EE前需要了解的问题
- 现在最需要深入了解的两个方向
- 浅谈我对集合框架的了解 - List
- 集合初始化需要注意的一个问题
- list嵌套需要注意的问题
- 深入分析集合List的排序Collections.sort
- 关于list集合的去重问题
- List集合remove元素的问题
- 从List转到另一个List需要注意的问题总结
- 数据库管理员需要深入了解内部威胁
- UIViewController的loadView需要了解和注意的问题
- 探究游戏的前期开发需要了解的问题~
- 孤儿进程、僵死进程
- Android轮播循环ViewPager
- 《java入门第一季》二维数组三个案例详解
- JS不间断横向滚动实现代码
- android回调实例
- list集合需要深入了解的问题
- 点是否在多边形内部 别人的博客
- android Json解析——揭开json解析的神秘面纱
- Neural Network, IDE
- phantomjs#3系统模块
- OpenStack Austin Nova Design Summit - Migration
- 一个adapter实现多个itemView
- untiy 3d ShaderLab_第6章_VertexLit渲染路径_2_顶点照明和Unity存放光源的第一种方式
- java-计算几何图形的面积(抽象类)