Java中ArrayList和SubList的坑面试题
作者:芝士味的椒盐 时间:2023-09-21 06:56:21
代码复现
不要,思考一下会打印出什么?
List<String> list1 = new ArrayList<>(Arrays.asList("username", "passwd"));
List<String> list2 = list1.subList(0, 2);
list2.add("email");
System.out.println(list1);
System.out.println(list2);
执行结果:
你是否感觉疑惑?在想为什么在list2添加的在list1也添加是吧?
源码解析
subList接口
List<E> subList(int fromIndex, int toIndex);
我们使用的是ArrayList,所以是选择ArrayList即可
public List<E> subList(int fromIndex, int toIndex) {
subListRangeCheck(fromIndex, toIndex, size);
return new SubList(this, 0, fromIndex, toIndex);
}
fromIndex是从List元素开始索引,toIndex是List元素结束索引,subListRangeCheck方法是检查是否在允许范围之内。
static void subListRangeCheck(int fromIndex, int toIndex, int size) {
//开始索引小于0
if (fromIndex < 0)
throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
//结束索引大于容量
if (toIndex > size)
throw new IndexOutOfBoundsException("toIndex = " + toIndex);
//开始索引大于结束索引
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +
") > toIndex(" + toIndex + ")");
重头戏在new SubList(this, 0, fromIndex, toIndex);这里,看看下面的SubList就会知道,this关键字将当前对象的引用也就是list1传入了SubList,把传入的list1变成parent赋值给SubList内部成员,然后又将这个构造生成的赋值给list2,也就是说list1和list2是引用了同一个对象,指向的是同一list。
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;
}
再来看看list2.add的源码,将元素直接添加在list1和list2共同的list引用对象上,这就是为什么list2添加了,list1也添加了。
public void add(int index, E e) {
rangeCheckForAdd(index);
checkForComodification();
//将元素直接添加在list1和list2共同的list引用对象上
parent.add(parentOffset + index, e);
this.modCount = parent.modCount;
this.size++;
}
附:ArrayList的subList简单介绍和使用
subList(int fromIndex, int toIndex);
它返回原来list的从[fromIndex, toIndex)之间这一部分其实就是list的子列表(注意:fromIndex是 [ 说明包括其本身,toIndex是 )说明不包括其本身)。
这个子列表的本质其实还是原列表的一部分;也就是说,修改这个子列表,将导致原列表也发生改变。
举例说明
list中包含1,2,3,4,5,6一共6个元素,list.subList(1,3)返回的是2,3(list以0为开始)
还有一个经常使用的list.subList(1,list.size)
list中包含1,2,3,4,5,6一共6个元素,list.subList(1,list.size)返回的是2,3,4,5,6(list以0为开始)
来源:https://blog.csdn.net/qq_45860349/article/details/122850216
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过特性(attribute)为枚举添加更多信息示例
java实现单链表之逆序
全网最深分析SpringBoot MVC自动配置失效的原因
![](https://img.aspxhome.com/file/2023/4/65214_0s.jpg)
利用Postman和Chrome的开发者功能探究项目(毕业设计项目)
![](https://img.aspxhome.com/file/2023/9/117519_0s.png)
Java 批量文件压缩导出并下载到本地示例代码
Android表格自定义控件使用详解
![](https://img.aspxhome.com/file/2023/7/138237_0s.jpg)
Android中WebView常见问题及解决方案汇总
![](https://img.aspxhome.com/file/2023/1/113311_0s.png)
简单实现Android闹钟功能
![](https://img.aspxhome.com/file/2023/6/108256_0s.jpg)
Android动画之小球拟合动画实例
![](https://img.aspxhome.com/file/2023/0/138090_0s.gif)
详解java CountDownLatch和CyclicBarrier在内部实现和场景上的区别
Java获取视频时长、大小的示例
java如何获得redis所有的key-value
![](https://img.aspxhome.com/file/2023/3/60823_0s.jpg)
java字符串比较获取字符串出现次数的示例
android开发之listView组件用法实例简析
![](https://img.aspxhome.com/file/2023/6/120166_0s.png)
C#4.0新特性之协变与逆变实例分析
Java使用BigDecimal进行高精度计算的示例代码
Android实现左滑删除控件
![](https://img.aspxhome.com/file/2023/6/106826_0s.gif)
Java MyBatis本地缓存原理详解
![](https://img.aspxhome.com/file/2023/3/79213_0s.webp)