最近在项目中遇到了一个问题,由一个对象序列化的结构,在反序列化时一直提示失败,真的百思不得其解啊。在对问题排查了好久之后,才发现是这个序列化的对象中的list调用了ArrayList的sublist方法存入导致的问题,真的是满满的坑,sublist还是要慎重使用的啊,下面详细介绍下sublist。

以下内容转自:ArrayList的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方法

    参考博文使用java.util.List.subList时最好小心点 List接口中定义: List<E> subList(int fromIndex, int toIndex); 英文注 ...

  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. List集合数据太多进行分批,List的subList方法应用

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

  8. iOS7之后经过滑动返回导航栏隐藏和显示带来的坑(转载)

    iOS7之后经过滑动返回导航栏隐藏和显示带来的坑 Apple 自从iOS7之后增加了屏幕边缘右滑返回交互的支持,再配合UINavigationController的交换动画,pop上一级的操作变的非常 ...

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

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

随机推荐

  1. python基础之六:编码简介以及python3中的编码

    1.常见的四种编码方式的编码过程: ascii A : 00000010 8位 一个字节 unicode A : 00000000 00000001 00000010 00000100 32位 四个字 ...

  2. Pandas | 20 级联

    Pandas提供了各种工具(功能),可以轻松地将Series,DataFrame和Panel对象组合在一起. pd.concat(objs,axis=0,join='outer',join_axes= ...

  3. Asp.Net报https请求报传输流收到意外的 EOF 或 0 个字节

    网上搜索,都说是.net framework版本太低,改为.net 4.5以上版本即可.于是使用vs2017环境测试了下,使用.net framework4.5版本确实可以正常返回结果,低于这个版本就 ...

  4. NOIP动态规划大合集

    1.前言 NOIP2003-2014动态规划题目大合集,有简单的也有难的(对于我这种动态规划盲当然存在难的),今天就把这些东西归纳一下,做一个比较全面的总结,方便对动态规划有一个更深的理解. 2.NO ...

  5. 【数位DP】【P2657】[SCOI2009]windy数

    Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为 \(2\) 的正整数被称为windy数. windy想知道, 在 \(A\) 和 \(B\) 之间,包括 ...

  6. LOJ2507 CEOI2011 Matching

    题目链接 参考了 神仙yyb的博客 现在发现kmp不仅能匹配字符串,还可以用于处理任意模式匹配中的状态,如这题中已经匹配的序列中的数的大小关系就是一种状态,使用kmp找到模式序列的每一个前缀的bord ...

  7. nginx配置文件解释

    #全局配置 # For more information on configuration, see:# * Official English Documentation: http://nginx. ...

  8. #lua中编写shader的方式

    lua中编写shader的方式 1. 字符串拼接 类似于下面这种 vertDefaultSource = "\n".."\n" .. "attribu ...

  9. zooKeeper使用记录

    背景:记录zooKeeper使用过程中遇到的问题. 在删除zooKeeper相关节点的时候需要进行权限的认证,下面的连接讲的还是很详细的 zookeeper的ACL权限控制

  10. Linux 教你如何预防删库到跑路

    学习linux之前,有几个命令是必须记住的 我们最常见的删库命令 rm -rf /** 这个命令可以在你心情不好的时候,对着公司服务器操作一番,保证让你一时爽, 删完库这个时候你就该跑路了,但是你真的 ...