
👨🏻🎓部落客介紹:大家好,我是芝士味的椒鹽,一名在校大學生,熱愛分享知識,很高興在這裡認識大家🌟
🌈擅長領域:Java、大資料、運維、電子
🙏🏻如果本文章各位小夥伴們有幫助的話,🍭關注+👍🏻點贊+🗣評論+📦收藏,相應的有空了我也會回訪,互助!!!
🤝另本人水準有限,旨在創作簡單易懂的文章,在文章描述時如有錯,懇請各位大佬指正,在此感謝!!!
@[TOC]
代碼複現
- 不要,思考一下會列印出什麼?
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++;
}