③ AbstractCollection中定义了MAX_ARRAY_SIZE, 表示数组可分配的最大容量, 原文:
The maximum size of array to allocate. Some VMs reserve some header words in an array.
Attempts to allocate larger arrays may result in OutOfMemoryError: Requested array size exceeds VM limit
④ AbstractCollection中存在两个private方法:
finishToArray: 在toArray()方法中使用, 当迭代器返回的元素比预期的多时,重新分配toArray中使用的数组,并从迭代器中完成数组填充。(Reallocates the array being used within toArray when the iterator returned more elements than expected, and finishes filling it from the iterator.)
publicabstractclassAbstractList<E> extendsAbstractCollection<E> implementsList<E> { protectedtransientintmodCount=0; protectedAbstractList() {} /* AbstractList中未实现的List接口方法 */ publicabstractEget(int index); /* AbstractList中实现的List接口(AbstractList他自己实现的) */ publicbooleanadd(E e) {...} publicEset(int index, E element) {...} publicvoidadd(int index, E element) {...} publicEremove(int index) {...} publicintindexOf(Object o) {...} publicintlastIndexOf(Object o) {...} publicvoidclear() {...} publicbooleanaddAll(int index, Collection<? extends E> c) {...} publicIterator<E> iterator() {...} publicListIterator<E> listIterator() {...} publicListIterator<E> listIterator(finalint index) {...} publicList<E> subList(int fromIndex, int toIndex) {...} /* 重写Collection中的方法 */ publicbooleanequals(Object o) {...} publicinthashCode() {...} /* AbstractList中定义的方法 */ staticvoidsubListRangeCheck(int fromIndex, int toIndex, int size) {...} protectedvoidremoveRange(int fromIndex, int toIndex) {...} privatevoidrangeCheckForAdd(int index) {...} privateStringoutOfBoundsMsg(int index) {...} /* AbstractList中的Iterator实现类, 真正List元素ListItr的父类 */ privateclassItrimplementsIterator<E> { /** * Index of element to be returned by subsequent call to next. */ intcursor=0; /** * Index of element returned by most recent call to next or * previous. Reset to -1 if this element is deleted by a call * to remove. */ intlastRet= -1; /** * The modCount value that the iterator believes that the backing * List should have. If this expectation is violated, the iterator * has detected concurrent modification. */ intexpectedModCount= modCount; /* Iterator接口中的方法 */ publicbooleanhasNext() {...} publicEnext() {...} publicvoidremove() {...} /* Itr中定义的方法 */ finalvoidcheckForComodification() {...} } /* AbstractList中Iterator真正的元素 */ privateclassListItrextendsItrimplementsListIterator<E> { ListItr(int index) { cursor = index; } /* ListIterator接口中的方法 */ publicbooleanhasPrevious() {...} publicEprevious() {...} publicintnextIndex() {...} publicintpreviousIndex() {...} publicvoidset(E e) {...} publicvoidadd(E e) {...} } /* JDK 8之后, 为了并行遍历元素而设计的一个可分割迭代器(splitable iterator) */ staticfinalclassRandomAccessSpliterator<E> implementsSpliterator<E> { privatefinal List<E> list; privateint index; // current index, modified on advance/split privateint fence; // -1 until used; then one past last index privatefinal AbstractList<E> alist; // The following fields are valid if covering an AbstractList privateint expectedModCount; // initialized when fence set /* RandomAccessSpliterator的构造方法 */ RandomAccessSpliterator(List<E> list) {...} /* Spliterator接口中的方法 */ publicSpliterator<E> trySplit() {...} publicbooleantryAdvance(Consumer<? super E> action) {...} publiclongestimateSize() {...} publicintcharacteristics() {...} publicvoidforEachRemaining(Consumer<? super E> action) {...} /* RandomAccessSpliterator类中定义的方法 */ RandomAccessSpliterator(List<E> list) {...} privateRandomAccessSpliterator(RandomAccessSpliterator<E> parent, int origin, int fence) {...} // Create new spliterator covering the given range privatestatic <E> Eget(List<E> list, int i) {...} privateintgetFence() {...} // initialize fence to size on first use } /* AbstractList的内部类SubList, 继承了AbstractList本身, 是List的一个视图 */ privatestaticclassSubList<E> extendsAbstractList<E> { privatefinal AbstractList<E> root; privatefinal SubList<E> parent; privatefinalint offset; protectedint size; /* AbstractList抽象类(内部类继承了包括他的类!)的方法 */ publicEset(int index, E element) {...} publicvoidadd(int index, E element) {...} publicEremove(int index) {...} protectedvoidremoveRange(int fromIndex, int toIndex) {...} publicbooleanaddAll(int index, Collection<? extends E> c) {...} publicIterator<E> iterator() {...} publicListIterator<E> listIterator(int index) {...} publicList<E> subList(int fromIndex, int toIndex) {...} publicEget(int index) {...} /* AbstractCollection抽象类中的方法 */ publicintsize() {...} // AbstractCollection并未实现该方法, 另一个未实现的方法是Iterator() publicbooleanaddAll(Collection<? extends E> c) {...} // 覆盖AbstractCollection的方法 /* SubList中定义的方法 */ publicSubList(AbstractList<E> root, int fromIndex, int toIndex) {...} // Constructs a sublist of an arbitrary AbstractList, which is not a SubList itself. protectedSubList(SubList<E> parent, int fromIndex, int toIndex) {...} // Constructs a sublist of another SubList. privatevoidrangeCheckForAdd(int index) {...} privateStringoutOfBoundsMsg(int index) {...} privatevoidcheckForComodification() {...} privatevoidupdateSizeAndModCount(int sizeChange) {...} } /* 继承自SubList(他的爸爸就是AbstractList), 并实现RandomAccess接口 */ privatestaticclassRandomAccessSubList<E> extendsSubList<E> implementsRandomAccess { /* 覆盖了父类SubList的方法 */ publicList<E> subList(int fromIndex, int toIndex) {...} /* RandomAccessSubList中定义的方法 */ RandomAccessSubList(AbstractList<E> root, int fromIndex, int toIndex) {...} // Constructs a sublist of an arbitrary AbstractList, which is not a RandomAccessSubList itself. RandomAccessSubList(RandomAccessSubList<E> parent, int fromIndex, int toIndex) {...} // Constructs a sublist of another RandomAccessSubList. } }
备注:
这里的关系比较复杂, 首先先说明一下RandomAccess接口:
RandomAccess接口:
它是JDK 1.4中加入的接口, 它内部一个方法都没定义, 是一个标志接口, 文档原文为:
Marker interface used by List implementations to indicate that they support fast (generally constant time) random access.
即RandomAccess 是一个标志接口,表明实现这个这个接口的 List 集合是支持快速随机访问的。也就是说,实现了这个接口的集合是支持 快速随机访问 策略的;