java List.subList方法中的陷阱

http://blog.163.com/wm_at163/blog/static/1321734902012526101318539/

ArrayList 中 subList 的基本用法: subList(fromIndex:int,toIndex:int):List 返回从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   } 

你曾经说,最大的愿望,

java List.subList方法中的陷阱

相关文章:

你感兴趣的文章:

标签云: