天天看點

java List.subList方法中的超級大陷阱

轉載位址:http://blog.163.com/wm_at163/blog/static/1321734902012526101318539/

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   
}