ArrayList的subList方法
参考博文使用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方法的更多相关文章
- ArrayList的subList方法带来的坑
最近在项目中遇到了一个问题,由一个对象序列化的结构,在反序列化时一直提示失败,真的百思不得其解啊.在对问题排查了好久之后,才发现是这个序列化的对象中的list调用了ArrayList的sublist方 ...
- 为什么要谨慎使用Arrays.asList、ArrayList的subList?
1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...
- 【Java 基础】Arrays.asList、ArrayList的subList注意事项
1. 使用Arrays.asList的注意事项 1.1 可能会踩的坑 先来看下Arrays.asList的使用: List<Integer> statusList = Arrays.asL ...
- 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- 谨慎使用ArrayList中的subList方法
转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...
- ArrayList.subList方法使用总结
ArrayList.subList方法使用总结 示例 List<String> list=new ArrayList<>(); list.add("d"); ...
- 需要注意的subList方法!和substring是不一样的!从源码解释他们的不同。
很多时候我们截取字符串用的是substring方法,很自然用着,但是对于列表的截取时很多时候就用得很少,但是其实他们是很不一样的,具体哪里不一样呢? package main; import java ...
- List集合数据太多进行分批,List的subList方法应用
List<String> mStrings=new ArrayList<>(); //初始化 for (int i = 0; i < 1020; i++) { mStri ...
- java List.subList方法中的超级大陷阱
ArrayList 中 subList 的基本用法: subList(fromIndex:int,toIndex:int):List<E> 返回从fromIndex到toindex-1 的 ...
随机推荐
- Linux sort uniq 命令。简单运用
-n #代表以数字方法排序,如果倒序加上-r -t ':' #-t指定分隔符-k ...
- console的所有用法
http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html 参考它. console.info(), console.debug() ...
- VSTO学习笔记
文档类型程序发布: 安装.NetFrameWork Visual Studio 2010 Tools for Office Runtime 4.0下载地址: http://www.microsoft. ...
- C++ 补遗
C++通过引用传递数组 数组形参可以声明为数组的引用.如果形参是数组的引用,编译器不会将数组实参转化为指针,而是传递数组的引用本身. 在这种情况下,数组大小成为形参和实参类型的一部分(实参长度与形参长 ...
- [ACM_数据结构] POJ2352 [树状数组稍微变形]
Description Astronomers often examine star maps where stars are represented by points on a plane and ...
- 排序算法之快速排序(Quicksort)解析
一.快速排序算法的优点,为什么称之为快排? Quicksort是对归并排序算法的优化,继承了归并排序的优点,同样应用了分治思想. 所谓的分治思想就是对一个问题“分而治之”,用分治思想来解决问题需要两个 ...
- winform NPOI excel 导出并选择保存文件路径
public void ExcelOp(DataGridView gdv,ArrayList selHead) { if (selHead.Count==0) { MessageBox.Show(&q ...
- 【CronExpression表达式详解和案例】(转载)
原文地址:https://www.cnblogs.com/pipi-changing/p/5697481.html 找了下Cron的资料,这篇作者写的比较清晰,转载记录一下,方便后面使用的时候在g ...
- Python对excel表格的操作.
参考博客: https://blog.csdn.net/lmj19851117/article/details/78814721 ####一.excel的读取操作xlrd#### import xlr ...
- linux 修改MTU值
Linux系统还可以通过如下方式查看.修改MTU值. 查看:cat /sys/class/net/eth0/mtu 设置:echo "1460" > /sys/class/n ...