ArrayList 中 subList 的基本用法:
subList(fromIndex:int,toIndex:int):List<E> | 返回從fromIndex到toindex-1 的 子列表 |
在使用集合中,可能常常需要取集合中的某一部分子集來進行一下操作,於是subList這個方法就映入我們的眼簾,毫不猶豫地使用。
例如以下代碼:
public static void main(final String[] args) { List<Object> lists = new ArrayList<Object>(); lists.add("1"); lists.add("2"); lists.add("3"); lists.add("4"); List<Object> tempList = lists.subList(2, lists.size()); tempList.add("6"); System.out.println(tempList); // 1 System.out.println(lists); // 2 }
代碼初步寫好后,可能我們想達到的效果是:往集合lists的子集合tempList中添加一個元素6,而原有的集合保持不變。
即到達這樣的效果:lists = [1, 2, 3, 4],tempList = [3, 4, 6]。但是我們看到實際的結果確是lists里邊也添加了元素6。
這是怎么一會事呢,通過查找java原代碼我們可以看到:tempList的subList實現代碼在AbstractList類里邊,然而無論如何,最終的結果都是返回一個AbstractList的子類:SubList(該類是一個使用默認修飾符修飾的類,其源代碼位於AbstractList.java類文件里邊),
SubList類的構造方法:
SubList(AbstractList<E> list, int fromIndex, int toIndex) { if (fromIndex < 0) throw new IndexOutOfBoundsException("fromIndex = " + fromIndex); if (toIndex > list.size()) throw new IndexOutOfBoundsException("toIndex = " + toIndex); if (fromIndex > toIndex) throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")"); l = list; offset = fromIndex; size = toIndex - fromIndex; expectedModCount = l.modCount; }
里邊,將我們原有的list對象給緩存到SubList類對象的一個屬性中去了。
而SubList類的add/remove等修改元素的方法中,都使用l進行了操作:
public void add(int index, E element) { if (index<0 || index>size) throw new IndexOutOfBoundsException(); checkForComodification(); l.add(index+offset, element); expectedModCount = l.modCount; size++; modCount++; }
因此,當我們使用子集合tempList進行元素的修改操作時,會影響原有的list集合。所以在使用subList方法時,一定要想清楚,是否需要對子集合進行修改元素而不影響原有的list集合。
如果需要對子集合的元素進行修改操作而不需要影響原集合時,我們可以使用以下方法進行處理:
public static void main(final String[] args) public static void main(final String[] args) { List<Object> lists = new ArrayList<Object>(); lists.add("1"); lists.add("2"); lists.add("3"); lists.add("4"); //注意這里是和本文頂部的代碼不同的.... List<Object> tempList = new ArrayList<Object>(lists.subList(2, lists.size())); tempList.add("6"); System.out.println(tempList); // 1 System.out.println(lists); // 2 }