ArrayList.subList注意点

平时经常用到ArrayList.subList进行list的截取,发现里面有一些注意点,记录一下

        List<Integer> oldList = new ArrayList<Integer>(){{add(1);add(2);add(3);}};
        List<Integer> newList = oldList.subList(1, 2);
  • SubList返回的是原list的视图,并不是创建了一个新的集合

public List<E> subList(int fromIndex, int toIndex) {
        subListRangeCheck(fromIndex, toIndex, size);
        return new SubList(this, 0, fromIndex, toIndex);
    }

其中 newSubList是ArrayList里的一个内部类

SubList(AbstractList<E> parent,
                int offset, int fromIndex, int toIndex) {
            this.parent = parent;
            this.parentOffset = fromIndex;
            this.offset = offset + fromIndex;
            this.size = toIndex - fromIndex;
            this.modCount = ArrayList.this.modCount;
        }

当访问例子中的newList时,是通过调用subList方法的入参作为偏移量来访问以及操作的,比如说get方法:

public E get(int index) {
            rangeCheck(index);
            checkForComodification();
            return ArrayList.this.elementData(offset + index);
        }
  • 修改原集合的值,会影响子集合,因为返回的是原集合的视图

  • 修改原集合的结构(add/remove等),遍历新集合时,会抛出ConcurrentModificationException异常

ArrayList中的modCount记录了该list结构性的修改次数,在内部类SubList进行任何操作时,都会校验SubList中的modCount与原集合的modCount是否一致,不一致的话就会抛出上面的

private void checkForComodification() {
        if (this.modCount != l.modCount)
            throw new ConcurrentModificationException();
    }
  • 修改子集合的值,会影响原集合,因为本质上修改的仍然是原来的集合

  • 修改子集合的结构,会影响原集合。

对子集合进行add操作时,会调用ArrayList的add方法,会将ArrayList中的modCount自增,同时同步到SubList中。remove类似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每年进步一点点

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值