1.Arrays.asList返回的并非是真正的Arraylist
//此处返回的ArrayList 是Arrays类的一个内部类,并非是 java.util包下Arraylist
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
//该类与util包下的都是继承 AbstractList<E>,但并为重写add,remove方法,而AbstractList<E>方法中的add,remove只会抛异常
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
2 Arraylist.subList()方法返回的List<E>并不能强转为Arraylist,add,remove操作的数据都是原始的Arraylist
List<String> stringList = new ArrayList<>();
stringList.add("大");
stringList.add("神");
stringList.add("不");
stringList.add("在");
//此处的强转会抛 java.util.ArrayList$SubList cannot be cast to java.util.ArrayList
List<String> subList = (ArrayList) stringList.subList(0, 2);
//add,remove操作原Arraylist的原因,parent.add
public void add(int index, E e) {
if (index < 0 || index > this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
parent.add(parentOffset + index, e);
this.modCount = parent.modCount;
this.size++;
}
3.Arraylist.subList ()返回的List<String>在原数据改变后,不能进行add,remove,遍历操作
private void checkForComodification() {
if (ArrayList.this.modCount != this.modCount)
throw new ConcurrentModificationException();
}