日期:2014-05-20  浏览次数:20828 次

我们爱分享----java中List.subList方法使用注意
说明:我写此的目的不是为了抨击jdk的不好,而是为了说明这个地方可能人为引发误解。
在使用集合中,可能常常需要取集合中的某一部分子集来进行一下操作,于是subList这个方法就映入我们的眼帘,毫不犹豫地使用。

例如以下代码:

Java code

    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类的构造方法:

Java code
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;  
}  


 

 因此,当我们使用子集合tempList进行元素的修改操作时,会影响原有的list集合。所以在使用subList方法时,一定要想清楚,是否需要对子集合进行修改元素而不影响原有的list集合。

如果需要对子集合的元素进行修改操作而不需要影响原集合时,我们可以使用以下方法进行处理:
Java code

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   
}  


------解决方案--------------------
List<Object> tempList = new ArrayList<Object>(lists.subList(2, lists.size())); 
感谢楼主,以后遇到了就这么操作
------解决方案--------------------
see java.util.List
Java code
    /**
     * Returns a view of the portion of this list between the specified
     * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive.  (If
     * <tt>fromIndex</tt> and <tt>toIndex</tt> 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.<p>

------解决方案--------------------
按照java.util.List的接口声明的契约,对subList返回的List进行的操作都会影响原来的List,反之亦然
------解决方案--------------------
探讨
按照java.util.List的接口声明的契约,对subList返回的List进行的操作都会影响原来的List,反之亦然