Java ArrayList集合元素正确的删除方式

范例1:

package com.contoso;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class MainApp {

    /**
     * 这是错误的ArrayList删除范例,条件判断里删除的是第2个元素执行失败,
     */
    public static void test1() {
        List<String> list = new ArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        for (String item : list) {
            if ("wuhan".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println("after remove list = " + list);
    }
    /**
     * 这是错误的ArrayList删除范例,条件判断里删除的是第1个元素能执行成功,不稳定的代码依然是错误的实现方式
     */
    public static void test2() {
        List<String> list = new ArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        for (String item : list) {
            if ("shanghai".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println("after remove list = " + list);
    }
    
    /**
     * 这是错误的ArrayList删除范例
     */
    public static void test3() {
        List<String> list = new ArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String item = (String) it.next();
            if ("wuhan".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println("after remove list = " + list);
    }
    
    /**
     * 这是正确的ArrayList删除范例
     */
    public static void test4() {
        List<String> list = new CopyOnWriteArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        for (String item : list) {
            if ("wuhan".equals(item)) {
                list.remove(item);
            }
        }
        System.out.println("after remove list = " + list);
    }
    
    /**
     * 这是正确的ArrayList删除范例
     */
    public static void test5() {
        List<String> list = new ArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String item = (String) it.next();
            if ("wuhan".equals(item)) {
                it.remove();
            }
        }
        System.out.println("after remove list = " + list);
    }
    /**
     * 这是正确的ArrayList删除范例
     */
    public static void test6() {
        List<String> list = new ArrayList<>();
        list.add("shanghai");
        list.add("wuhan");
        System.out.println("before remove list = " + list);
        list.removeIf("wuhan"::equals);
        System.out.println("after remove list = " + list);
    }

    public static void main(String[] args) {
        test1();
    }
}

test1() method Console output :

run:
before remove list = [shanghai, wuhan]
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at com.contoso.MainApp.test1(MainApp.java:18)
	at com.contoso.MainApp.main(MainApp.java:105)
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 5 seconds)

test2() method Console output ,though it run successfully, it 's wrong code:

run:
before remove list = [shanghai, wuhan]
after remove list = [wuhan]
BUILD SUCCESSFUL (total time: 7 seconds)

 test3() method Console output :

run:
before remove list = [shanghai, wuhan]
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
	at java.util.ArrayList$Itr.next(ArrayList.java:859)
	at com.contoso.MainApp.test3(MainApp.java:51)
	at com.contoso.MainApp.main(MainApp.java:105)
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\ZiGoo\AppData\Local\NetBeans\Cache\10.0\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 0 seconds)

 test4() method Console output :

run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)

test5() method Console output :

run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)

test6() method Console output :

run:
before remove list = [shanghai, wuhan]
after remove list = [shanghai]
BUILD SUCCESSFUL (total time: 0 seconds)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值