Java|List.subList 踩坑小记

2023-09-21 22:33:09

很久以前在使用 Java 的 List.subList 方法时踩过一个坑,当时记了一条待办,要写一写这事,今天完成它。

我们先来看一段代码:

// 初始化 list 为 { 1, 2, 3, 4, 5 }
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
    list.add(i);
}

// 取前 3 个元素作为 subList,操作 subList
List<Integer> subList = list.subList(0, 3);
subList.add(6);

System.out.println(list.size());

输出是 5 还是 6

没踩过坑的我,会回答是 5,理由是:往一个 List 里加元素,关其它 List 什么事?

而掉过坑的我,口中直呼 666。

好了不绕弯子,我们直接看下 List.subList 方法的注释文档:

/**
 * Returns a view of the portion of this list between the specified
 * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.  (If
 * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
 * empty.)  The returned list is backed by this list, so non-structural
 * changes in the returned list are reflected in this list, and vice-versa.
 * The returned list supports all of the optional list operations supported
 * by this list.<p>
 *
 * This method eliminates the need for explicit range operations (of
 * the sort that commonly exist for arrays).  Any operation that expects
 * a list can be used as a range operation by passing a subList view
 * instead of a whole list.  For example, the following idiom
 * removes a range of elements from a list:
 * <pre>{@code
 *      list.subList(from, to).clear();
 * }</pre>
 * Similar idioms may be constructed for <tt>indexOf</tt> and
 * <tt>lastIndexOf</tt>, and all of the algorithms in the
 * <tt>Collections</tt> class can be applied to a subList.<p>
 *
 * The semantics of the list returned by this method become undefined if
 * the backing list (i.e., this list) is <i>structurally modified</i> in
 * any way other than via the returned list.  (Structural modifications are
 * those that change the size of this list, or otherwise perturb it in such
 * a fashion that iterations in progress may yield incorrect results.)
 *
 * @param fromIndex low endpoint (inclusive) of the subList
 * @param toIndex high endpoint (exclusive) of the subList
 * @return a view of the specified range within this list
 * @throws IndexOutOfBoundsException for an illegal endpoint index value
 *         (<tt>fromIndex &lt; 0 || toIndex &gt; size ||
 *         fromIndex &gt; toIndex</tt>)
 */
List<E> subList(int fromIndex, int toIndex);

这里面有几个要点:

  1. subList 返回的是原 List 的一个 视图,而不是一个新的 List,所以对 subList 的操作会反映到原 List 上,反之亦然;

  2. 如果原 List 在 subList 操作期间发生了结构修改,那么 subList 的行为就是未定义的(实际表现为抛异常)。

第一点好理解,看到「视图」这个词相信大家就都能理解了。我们甚至可以结合 ArrayList 里的 SubList 子类源码进一步看下:

private class SubList extends AbstractList<E> implements RandomAccess {
    private final AbstractList<E> parent;
    // ...

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

    public E set(int index, E e) {
        // ...
        checkForComodification();
        // ...
        ArrayList.this.elementData[offset + index] = e;
        // ...
    }

    public E get(int index) {
        // ...
        checkForComodification();
        return ArrayList.this.elementData(offset + index);
    }

    public void add(int index, E e) {
        // ...
        checkForComodification();
        parent.add(parentOffset + index, e);
        this.modCount = parent.modCount;
        // ...
    }

    public E remove(int index) {
        // ...
        checkForComodification();
        E result = parent.remove(parentOffset + index);
        this.modCount = parent.modCount;
        // ...
    }

    private void checkForComodification() {
        if (ArrayList.this.modCount != this.modCount)
            throw new ConcurrentModificationException();
    }

    // ...
}

可以看到几乎所有的读写操作都是映射到 ArrayList.this、或者 parent(即原 List)上的,包括 sizeaddremovesetgetremoveRangeaddAll 等等。

第二点,我们在文首的示例代码里加上两句代码看现象:

list.add(0, 0);
System.out.println(subList);

System.out.println 会抛出异常 java.util.ConcurrentModificationException

我们还可以试下,在声明 subList 后,如果对原 List 进行元素增删操作,然后再读写 subList,基本都会抛出此异常。

因为 subList 里的所有读写操作里都调用了 checkForComodification(),这个方法里检验了 subList 和 List 的 modCount 字段值是否相等,如果不相等则抛出异常。

modCount 字段定义在 AbstractList 中,记录所属 List 发生 结构修改 的次数。结构修改 包括修改 List 大小(如 add、remove 等)、或者会使正在进行的迭代器操作出错的修改(如 sort、replaceAll 等)。

好了小结一下,这其实不算是坑,只是 不应该仅凭印象和猜测,就开始使用一个方法,至少花一分钟认真读完它的官方注释文档


如果读完文章有收获,可以关注我的微信公众号「闷骚的程序员」并🌟设为星标🌟,随时阅读更多内容。

帮忙点个「分享」或者「在看」吧!

⬇️⬇️⬇️

更多推荐

百度SEO不稳定的原因及解决方法(百度SEO不稳定因素的5大包括)

百度SEO优化不稳定介绍:蘑菇号-www.mooogu.cn随着百度SEO算法的不断变化和升级,许多网站的SEO排名经常出现不稳定的情况,这种情况在一定程度上影响了网站的流量和排名,导致网站的质量评分降低。因此,深入分析百度SEO不稳定的原因和解决方法非常必要。百度SEO不稳定因素的5大包括:1.网站内容和质量不佳,没

day06_Java中的流程控制语句

流程控制简单来讲所谓流程就是完成一件事情的多个步骤组合起来就叫做一个流程。在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的。我们必须清楚每条语句的执行流程。而且,很多时候要通过控制语句的执行顺序来实现我们想要的功能。流程控制语句分为:顺序结构丶分支结构(if,switch)丶循环结构(for,whi

SpringMvc决战-【SpringMVC之自定义注解】

目录一、前言1.1.什么是注解1.2.注解的用处1.3.注解的原理二.注解父类1.注解包括那些2.JDK基本注解3.JDK元注解4.自定义注解5.如何使用自定义注解(包括:注解标记【没有任何东西】,元数据注解)?三.实例展示1.案例一(获取类与方法上的注解值)1.1导入一个类用于创造实例1.2导入三个注解1.3导入测试

CSAPP的Lab学习——CacheLab

文章目录前言一、A部分:编写一个高速缓存模拟器构造高速缓冲行结构仿写主函数,使用getopt()函数分配空间并释放读取给的trace文件模拟cache行为二、B部分:优化矩阵转置32*32矩阵转置64*64矩阵转置61*67矩阵转置总结前言一个本硕双非的小菜鸡,备战24年秋招。刚刚看完CSAPP,真是一本神书啊!遂尝试

MyBatis友人帐之ResultMap及分页

一、ResultMap1.1查询为null问题要解决的问题:属性名和字段名不一致解决方案方案一:为列名指定别名,别名和java实体类的属性名一致.<selectid="selectUserById"resultType="User">selectid,name,pwdaspasswordfromuserwhereid=

OpenCV自学笔记十七:傅里叶变换

1、Numpy实现傅里叶变换傅里叶变换(FourierTransform)是一种将信号从时域转换到频域的数学变换。它将一个连续或离散的时域信号分解为一组正弦和余弦函数的复合。在Python中,可以使用NumPy库来实现傅里叶变换。具体步骤如下:1.导入NumPy库:importnumpyasnp2.准备输入信号数据,可

9月21日,每日信息差

今天是2023年9月21日,以下是为您准备的14条信息差第一、谷歌高管已经广泛讨论了在2027年之前将博通作为人工智能芯片供应商的可能性第二、清华系团队宣布研发出千亿参数“制药版ChatGPT”,覆盖药物立项、临床前研究、临床试验的各阶段,作为制药专家的得力AI助手,提升药物研发效率。团队还发布了全球首个千亿参数多模态

手机快充协议

高通:QC2.0、QC3.0、QC3.5、QC4.0、QC5.0、FCP、SCP、AFC、SFCP、MTKPE1.1/PE2.0/PE3.0、TYPEC、PD2.0、PD3.0/3.1、VOOC支持PD3.0/PD2.0支持QC3.0/QC2.0支持AFC支持FCP支持PE2.0/PE1.1联发科的PE(PumpExp

单元测试(基于安卓项目)总结

前言:负责公司的单元测试体系的搭建,大约有一两个月的时间了,从最初的框架的调研,到中期全员的培训,以及后期对几十个项目单元测试的引入和推进,也算是对安卓的单元测试有了一些初步的收获以及一些新的认知,因此写下这篇文章来进行一个记录和总结。以下的所有内容纯属个人观点,欢迎讨论。一.单元测试标准1.测试维度单元测试有很多维度

第八篇-Tesla P40+ChatGLM2+LoRA

部署环境系统:CentOS-7CPU:14C28T显卡:TeslaP4024G驱动:515CUDA:11.7cuDNN:8.9.2.26目的验证P40部署可行性,只做验证学习lora方式微调创建环境condacreate--nameglm-tuningpython=3.10condaactivateglm-tuning

百分点科技跻身中国智慧应急人工智能解决方案市场前三

近日,全球领先的IT市场研究和咨询公司IDC发布了《中国智慧应急解决方案市场份额,2022》报告,数据显示,2022年中国智慧应急整体市场为104亿元人民币。其中,智慧应急人工智能解决方案子市场备受关注,百分点科技以10%的市场份额居该市场第三。报告指出,从数字技术应用上看,技术之间融合以及技术与场景的融合应用成为可见

热文推荐