ArrayList底层原理(个人理解),集合遍历修改的并发错误问题

文章详细介绍了ArrayList的底层实现,它基于动态数组,查询快速但修改较慢。在遍历并修改集合时,普通for循环可以实现,增强for循环和forEach在修改时会导致问题,而使用Iterator迭代器则能安全地进行删除操作。
摘要由CSDN通过智能技术生成

ArrayList底层原理

首先ArrayList的底层是使用动态数组来进行元素的存储。
优点:查询快,修改相对较慢
创建ArrayList时,底层会先创建一个长度为10的数组,使用一个size记录存储个数和下一个存储位置,当size大小等于数组长度时,数组将进行扩容,底层使用Arrays.copyOf()方法,每次扩容1.5倍。

在这里插入图片描述

集合遍历修改

for 循环

普通for循环配合自减操作可以完成集合的遍历修改

public class Test {

    public static void main(String[] args) {


        List<String> collection = new ArrayList<>();
        collection.add("sss");
        collection.add("sss");
        collection.add("sssss");
        collection.add(3,"sww");

        Object[] strings = collection.toArray();

//        collection.sort(Comparator.comparing(new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return -s.length();
//            }
//        }).reversed());

        for (int i = 0; i< collection.size();i++){
            if (collection.get(i).equals("sss")){
                collection.remove("sss");
                i--;
            }
        }

        System.out.println(collection);
    }
}

增强for循环

会出现相关错误
在这里插入图片描述

public class Test {

    public static void main(String[] args) {


        List<String> collection = new ArrayList<>();
        collection.add("sss");
        collection.add("sss");
        collection.add("sssss");
        collection.add(3,"sww");

        Object[] strings = collection.toArray();

//        collection.sort(Comparator.comparing(new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return -s.length();
//            }
//        }).reversed());

        for (String s:collection){
            if (s.equals("sss")){
                collection.remove("sss");
            }
        }
        System.out.println(collection);
    }
}

forEach

底层原理就是增强for循环,同样会出现问题

public class Test {

    public static void main(String[] args) {


        List<String> collection = new ArrayList<>();
        collection.add("sss");
        collection.add("sss");
        collection.add("sssss");
        collection.add(3,"sww");

        Object[] strings = collection.toArray();

//        collection.sort(Comparator.comparing(new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return -s.length();
//            }
//        }).reversed());
        collection.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                if ("sss".equals(s)){
                    collection.remove("sss");
                }
            }
        });
        System.out.println(collection);
    }
}

Iterator迭代器

直接使用集合进行删除(报错)
使用Iterator的remove方法不会报错


public class Test {

    public static void main(String[] args) {


        List<String> collection = new ArrayList<>();
        collection.add("sss");
        collection.add("sss");
        collection.add("sssss");
        collection.add(3,"sww");

        Object[] strings = collection.toArray();

//        collection.sort(Comparator.comparing(new Function<String, Integer>() {
//            @Override
//            public Integer apply(String s) {
//                return -s.length();
//            }
//        }).reversed());
        Iterator<String> iterator = collection.iterator();
        while (iterator.hasNext()){
            if ("sss".equals(iterator.next())){
//                collection.remove("sss");  错误的方法,会引发错误
                iterator.remove();
            }
        }


        System.out.println(collection);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值