日期:2014-05-20 浏览次数:20949 次
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 }
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; }
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 }
/** * 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,反之亦然
------解决方案--------------------