List 删除元素的方法


Java的List在删除元素时,一般会用 list.remove(o)/remove(i)方法。在使用时,容易触碰陷阱,得到意想不到的结果。总结以往经验,记录下来与大家分享。
初始化数组

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(4);
    list.add(5);
    System.out.println("初始化数组:"+list);// 初始化数组:[1, 2, 3, 3, 4, 5]
}

普通for循环

遍历List删除指定元素 ➡ 错误

public static void test(List<Integer> list){
    
    
    for(int i=0;i<list.size();i++){
    
    
        if(list.get(i)==3) {
    
    
            list.remove(i);
        }
    }
    System.out.println("for循环删除结果:"+list);// for循环删除结果:[1, 2, 3, 4, 5]
}

List调用remove(index)方法后,会移除index位置上的元素,index之后的元素就全部依次左移,即索引依次-1,要保证能操作所有的数据,需要把index-1,否则原来索引为index+1的元素就无法遍历到(因为原来索引为index+1的数据,在执行移除操作后,索引变成index了,如果没有index-1的操作,就不会遍历到该元素,而是遍历该元素的下一个元素)。
在这里插入图片描述
所以说使用for循环删除List元素,删除元素后同步调整索引或者倒序遍历删除元素才是可行的

删除元素后同步调整索引 ➡ 正确

public static void test2(List<Integer> list){
    
    
    for(int i=0;i<list.size();i++){
    
    
        if(list.get(i)==3) {
    
    
            list.remove(i--);
        }
    }
    System.out.println("for循环删除结果后调整索引:"+list);// for循环删除结果后调整索引:[1, 2, 4, 5]
}

倒序遍历删除元素 ➡ 正确

public static void test3(List<Integer> list){
    
    
    for(int i=list.size()-1;i>=0;i--){
    
    
        if(list.get(i)==3){
    
    
            list.remove(i);
        }
    }
    System.out.println("倒叙for循环删除结果"+list);// 倒叙for循环删除结果[1, 2, 4, 5]
}

foreach遍历

foreach遍历List删除元素 ➡ 错误

抛出异常:java.util.ConcurrentModificationException

foreach 写法实际上是对的 IterablehasNextnext方法的简写。因此从List.iterator()源码着手分析,跟踪iterator()方法,该方法返回了 Itr 迭代器对象。

通过代码我们发现 Itr 是 ArrayList 中定义的一个私有内部类,在 nextremove方法中都会调用checkForComodification 方法,该方法的 作用是判断 modCount != expectedModCount是否相等,如果不相等则抛出ConcurrentModificationException异常。

每次正常执行 remove 方法后,都会对执行expectedModCount = modCount赋值,保证两个值相等,那么问题基本上已经清晰了,在 foreach 循环中执行 list.remove(item);,对 list 对象的 modCount 值进行了修改,而 list 对象的迭代器的 expectedModCount 值未进行修改,因此抛出了ConcurrentModificationException异常。

迭代

迭代删除List元素 ➡ 正确

java中所有的集合对象类型都实现了Iterator接口,遍历时都可以进行迭代:

public static void test5(List<Integer> list){
    
    
    Iterator<Integer> it=list.iterator();
    while(it.hasNext()){
    
    
        if(it.next()==3){
    
    
            it.remove();
        }
    }
    System.out.println("迭代删除List元素:"+list);// 迭代删除List元素:[1, 2, 4, 5]
}

Iterator.remove() 方法会在删除当前迭代对象的同时,会保留原来元素的索引。所以用迭代删除元素是最保险的方法

迭代遍历,用list.remove(i)方法删除元素 ➡ 错误

public static void test6(List<Integer> list){
    
    
    Iterator<Integer> it=list.iterator();
    while(it.hasNext()){
    
    
        Integer value=it.next();
        if(value==3){
    
    
            list.remove(value);
        }
    }
    System.out.println("迭代遍历,用list.remove(i)方法删除元素:"+list);
}

抛出异常:java.util.ConcurrentModificationException,原理同foreach遍历List删除元素

Collection.removeIf()方法删除 ➡ 正确

removeIf()方法用于删除所有满足特定条件的数组元素
removeIf()方法的语法为:arraylist.removeIf(Predicate<E> filter)

注意:arraylist 是 ArrayList 类的一个对象
参数说明:filter - 过滤器,判断元素是否要删除
返回值:如果元素被删除则返回 true。

public static void test7(List<Integer> list){
    
    
    list.removeIf(item->{
    
    /*其他逻辑*/return item==3;});
    System.out.println("Collection.removeIf()方法删除:"+list); // Collection.removeIf()方法删除:[1, 2, 4, 5]
}

JDK1.8中,Collection以及其子类新加入了removeIf()方法,作用是按照一定规则过滤集合中的元素

使用方法:Collection之removeif()方法使用removeIf用法

其他

List删除元素时,注意Integer类型和int类型的区别

直接删除元素2,代码如下:

list.remove(2);
System.out.println(list); //

可以看出,List删除元素时传入数字时,默认按索引删除。如果需要删除Integer对象,调用remove(object)方法,需要传入Integer类型,代码如下:

list.remove(new Integer(2));
System.out.println(list); // [1, 3, 3, 4]

总结

  1. 用for循环遍历List删除元素时,需要注意元素会左移的问题
  2. List删除元素时,为避免陷阱,建议使用迭代器iterator的remove()方式或是removeIf()
  3. List删除元素时,默认按索引删除,而不是对象删除

猜你喜欢

转载自blog.csdn.net/qq_42700109/article/details/131339244