zoukankan      html  css  js  c++  java
  • java List 数组删除元素

    在 java 中,ArrayList 是一个很常用的类,在编程中经常要对 ArrayList 进行增、删、改、查操作。之前在学校时一直认为删除操作是最简单的,现在才越发觉得自己愚蠢。只需要设置好预期条件的查询才是最简单的,删除涉及到存储空间的释放,以及数组的遍历等问题,在list的操作中相对还算小老哥呢。

    这两天在给小程序提供后台接口,因为设计的改变,需要对于已查询出来的数组进行遍历删除。在使用 remove 方法对 ArrayList 进行删除操作时,报 java.util.ConcurrentModificationException 异常,下面探讨一下该异常的原因以及解决办法。

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            List<String> listA = new ArrayList<>();
            listA.add("a");
            listA.add("b");
            listA.add("c");
            listA.add("d");
            listA.add("e");
            listA.add("f");
            
            for(String str:listA){
                if (str == "c") {
                    listA.remove(str);
                }
            }
        }
    }

    运行代码发现,在调用 listA.remove(str) 时,会报 java.util.ConcurrentModificationException异常,如下图:

    Exception in thread "main" java.util.ConcurrentModificationException
    2     at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    3     at java.util.ArrayList$Itr.next(ArrayList.java:851)
    4     at com.zhang.Test.main(Test.java:19)

    debug 单步发现,在删除倒数第二个元素是,是不会报上面的错误的,除此之外都会报异常。既然发现了规律,那么可以从源码去寻找原因。

    我们可以发现 java for循环的实现,是将List 对象托管给迭代器 Iterator,即如果想对List 进行删除操作,均需要经过 Iterator,否则在遍历时会乱掉,故抛出 ConcurrentModificationException 异常。

    那么,我们可以看一下 Iterator 迭代器迭代的步骤:

    1、判断是否有下个元素:iterator.hasNext() 

     public boolean hasNext() {
                return cursor != size;
            }

    2、获取下个元素并赋值给上面例子中的item变量:item = iterator.next()

    public E next() {
                checkForComodification();
                int i = cursor;
                if (i >= size)
                    throw new NoSuchElementException();
                Object[] elementData = ArrayList.this.elementData;
                if (i >= elementData.length)
                    throw new ConcurrentModificationException();
                cursor = i + 1;
                return (E) elementData[lastRet = i];
            }

    经过调试发现,checkForComodification时返回了异常,异常原因为 modCount != expectedModCount:

    final void checkForComodification() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
            }

    继续跟源码,发现:

      a.modCount 是 List 从被 new 新建之后被修改的次数,当 List 调用 remove()等方法时,modCount++;

      b.expectedModCount 是 Itreator 期望当前的List被修改的次数;

      c.在Iterator初始化的时候将modCount 的值赋给了expectedModCount;

    那么,现在就很容易可以知道为什么会报异常了:

      1).modCount 会随着调用List.remove方法而自动增减,而expectedModCount则不会变化,就导致modCount != expectedModCount;

      2).在删除倒数第二个元素后,cursor=size-1,此时size=size-1,导致 hasNext 方法认为遍历结束;

    解决方案:

    经过查阅源码可以发现,iterator 也有一个 remove 方法,其中有一个重要的操作为 expectedModCount = modCount 这样就保证了两者的相等。

    public void remove() {
                if (lastRet < 0)
                    throw new IllegalStateException();
                checkForComodification();
    
                try {
                    ArrayList.this.remove(lastRet);
                    cursor = lastRet;
                    lastRet = -1;
                    expectedModCount = modCount;
                } catch (IndexOutOfBoundsException ex) {
                    throw new ConcurrentModificationException();
                }
            }

    修改后的代码如下:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            List<String> listA = new ArrayList<>();
            listA.add("a");
            listA.add("b");
            listA.add("c");
            listA.add("d");
            listA.add("e");
            listA.add("f");
            Iterator<String> it_b=listA.iterator();
            while(it_b.hasNext()){
                String a=it_b.next();
                if ("c".equals(a)) {
                    it_b.remove();
                }
            }
    } 
    }

    改完之后,启动 run,运行顺畅无比,借金星小姐姐(or 小哥哥??)的话作为结尾:完美~~

    参考:

    java.util.ConcurrentModificationException异常原因及解决方法

  • 相关阅读:
    IOS开发-CALayer和UIView详细汇总
    IOS开发-第三方SDWebImage下载网络图片的使用
    解决TalbleView头部或底部子控件不显示问题
    node.js http.get 和http.post 数据
    Node.js Express 获取request原始数据
    个人开发者做一款Android App需要知道的事情
    个人开发者的酸甜苦辣
    码农的福利来了, 编程在线Androd 客户端上线了
    console使用技巧
    AngularJS 常用语法
  • 原文地址:https://www.cnblogs.com/lilala-world/p/10437137.html
Copyright © 2011-2022 走看看