An optimized version of AbstractList.ListItr privateclassListItrextendsItrimplementsListIterator<E> { //唯一构造器:把index给cursor ListItr(int index) { super(); cursor = index; } //判断是否有前驱 publicbooleanhasPrevious(){ return cursor != 0; } //获取当前元素的索引 publicintnextIndex(){ return cursor; } 获取前驱的索引 publicintpreviousIndex(){ return cursor - 1; } public E previous(); publicvoidset(E e); publicvoidadd(E e); }
previous()
获取前驱
1 2 3 4 5 6 7 8 9 10 11
public E previous(){ checkForComodification(); int i = cursor - 1; if (i < 0) thrownew NoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownew ConcurrentModificationException(); cursor = i; return (E) elementData[lastRet = i]; }
set(E e)
调用ArrayList.set,其本身不会修改 modCount值
1 2 3 4 5 6 7 8 9 10 11
publicvoidset(E e){ if (lastRet < 0) thrownew IllegalStateException(); checkForComodification();