1. public List<E> subList(int fromIndex, int toIndex) {
    2. subListRangeCheck(fromIndex, toIndex, size);
    3. return new SubList(this, 0, fromIndex, toIndex);
    4. }
    1. private class SubList extends AbstractList<E> implements RandomAccess {
    2. private final AbstractList<E> parent;
    3. private final int parentOffset;
    4. private final int offset;
    5. int size;
    6. SubList(AbstractList<E> parent,
    7. int offset, int fromIndex, int toIndex) {
    8. this.parent = parent;
    9. this.parentOffset = fromIndex;
    10. this.offset = offset + fromIndex;
    11. this.size = toIndex - fromIndex;
    12. this.modCount = ArrayList.this.modCount;
    13. }
    14. public E set(int index, E e) {
    15. rangeCheck(index);
    16. checkForComodification();
    17. E oldValue = ArrayList.this.elementData(offset + index);
    18. ArrayList.this.elementData[offset + index] = e;
    19. return oldValue;
    20. }
    21. }
    22. SubListset()方法,是直接修改ArrayListelementData数组的,使用中应该注意
    23. SubList是没有实现Serializable接口的,是不能序列化的