参考博文使用java.util.List.subList时最好小心点

List接口中定义:

List<E> subList(int fromIndex, int toIndex);

英文注释:

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex 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.
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:
list.subList(from, to).clear(); Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified 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.)

根据注释得知:

1,该方法返回的是父list的一个视图,从fromIndex(包含),到toIndex(不包含)。fromIndex=toIndex 表示子list为空

2,父子list做的非结构性修改(non-structural changes)都会影响到彼此:所谓的“非结构性修改”,是指不涉及到list的大小改变的修改。相反,结构性修改,指改变了list大小的修改。

3,对于结构性修改,子list的所有操作都会反映到父list上。但父list的修改将会导致返回的子list失效。

4,tips:如何删除list中的某段数据:

list.subList(from, to).clear();

示例代码:

来自【Java每日一题】20170105,就是看到这个题目才让我知道list的这个方法我没有接触过

package ques;  

import java.util.ArrayList;
import java.util.List; public class Ques0105 { public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a"); // 使用构造器创建一个包含list的列表list1
List<String> list1 = new ArrayList<String>(list);
// 使用subList生成与list相同的列表list2
List<String> list2 = list.subList(0, list.size());
list2.add("b"); System.out.println(list.equals(list1));
System.out.println(list.equals(list2));
}
}

返回结果如下:

可以发现,list2为list的子list,当list2发生结构性修改(list2.add("b"))后,list也发生相应改变,所以返回结果为false和true

ArrayList的subList方法的更多相关文章

  1. ArrayList的subList方法带来的坑

    最近在项目中遇到了一个问题,由一个对象序列化的结构,在反序列化时一直提示失败,真的百思不得其解啊.在对问题排查了好久之后,才发现是这个序列化的对象中的list调用了ArrayList的sublist方 ...

  2. 为什么要谨慎使用Arrays.asList、ArrayList的subList?

    1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...

  3. 【Java 基础】Arrays.asList、ArrayList的subList注意事项

    1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...

  4. 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法

    GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...

  5. 谨慎使用ArrayList中的subList方法

    转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...

  6. ArrayList.subList方法使用总结

    ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...

  7. 需要注意的subList方法!和substring是不一样的!从源码解释他们的不同。

    很多时候我们截取字符串用的是substring方法,很自然用着,但是对于列表的截取时很多时候就用得很少,但是其实他们是很不一样的,具体哪里不一样呢? package main; import java ...

  8. List集合数据太多进行分批,List的subList方法应用

    List<String> mStrings=new ArrayList<>(); //初始化 for (int i = 0; i < 1020; i++) { mStri ...

  9. java List.subList方法中的超级大陷阱

    ArrayList 中 subList 的基本用法: subList(fromIndex:int,toIndex:int):List<E> 返回从fromIndex到toindex-1 的 ...

随机推荐

  1. bootstrap 问题

    less; sass: css预处理:可以直接使用.css,也可以修改.less,生成定制化的css CDN: 服务,使用这个效果会更好.theme一般不引入,jquery一般在js之前引入. 使用b ...

  2. Hibernate 注解和配置文件两种方法的对比(有实例)

    hibernate多对多形式(User类<---->Educate类) 1.基于注解的形式: User类: package com.ssh.entities; import java.ut ...

  3. Python学习-40.Python中的迭代

    在上一篇中,我们使用了生成器来创建了一个可遍历的对象.在其中,我们使用了yield关键字. Python我也正在学习中,因此对yield的本质我并不熟悉,但是,在C#中,yield关键字则是语法糖,其 ...

  4. SQL server经验分享:SQLSERVER 被标记为“可疑”的数据库处理方法

    --MyDB为修复的数据名USE MASTER GO SP_CONFIGURE 'ALLOW UPDATES',1 RECONFIGURE WITH OVERRIDE GO ALTER DATABAS ...

  5. jQuery之noConflict() 方法

    jQuery 核心 - noConflict() 方法,运行这个函数将变量 $ 的控制权让渡给第一个实现它的那个库.这有助于确保jQuery不会与其他库的$对象发生冲突. noConflict() 方 ...

  6. javascript中的with关键字

    说起js中的with关键字,很多小伙伴们的第一印象可能就是with关键字的作用在于改变作用域,然后最关键的一点是不推荐使用with关键字.听到不推荐with关键字后,我们很多人都会忽略掉with关键字 ...

  7. MySqlException: The user specified as a definer ('root'@'%') does not exist解决方法

    之前因为MySql安全问题,将root@%改为允许特定ip段进行远程连接,结果有一个接口报The user specified as a definer ('root'@'%') does not e ...

  8. 【加密算法】3DES

    一.简介 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES加密算 ...

  9. NetCore入门篇:(二)Net Core项目创建

    一.新建项目 1.选择菜单:文件 -> 新建 -> 项目 2.选择模板:NET Core -> ASP.NET Core Web 应用程序,输入名称 3.选择框架:ASP.NET C ...

  10. C# 一些代码小结--串口操作

    串口解析显示中文 private String SerialPortReadStr() { try { String str = null; int n = serialPort1.BytesToRe ...