/** * An optimized version of AbstractList.Itr */ privateclassItrimplementsIterator<E> { int cursor; // index of next element to return intlastRet= -1; // index of last element returned; -1 if no such intexpectedModCount= modCount;
publicbooleanhasNext() { return cursor != size; }
@SuppressWarnings("unchecked") public E next() { checkForComodification(); inti= cursor; if (i >= size) thrownewNoSuchElementException(); Object[] elementData = ArrayList.this.elementData; if (i >= elementData.length) thrownewConcurrentModificationException(); cursor = i + 1; return (E) elementData[lastRet = i]; }
publicvoidremove() { if (lastRet < 0) thrownewIllegalStateException(); checkForComodification();
privatestaticclassCOWIterator<E> implementsListIterator<E> { /** Snapshot of the array */ privatefinal Object[] snapshot; /** Index of element to be returned by subsequent call to next. */ privateint cursor;
@SuppressWarnings("unchecked") public E next() { if (! hasNext()) thrownewNoSuchElementException(); return (E) snapshot[cursor++]; }
@SuppressWarnings("unchecked") public E previous() { if (! hasPrevious()) thrownewNoSuchElementException(); return (E) snapshot[--cursor]; }
publicintnextIndex() { return cursor; }
publicintpreviousIndex() { return cursor-1; }
/** * Not supported. Always throws UnsupportedOperationException. * @throws UnsupportedOperationException always; <tt>remove</tt> * is not supported by this iterator. */ publicvoidremove() { thrownewUnsupportedOperationException(); }
/** * Not supported. Always throws UnsupportedOperationException. * @throws UnsupportedOperationException always; <tt>set</tt> * is not supported by this iterator. */ publicvoidset(E e) { thrownewUnsupportedOperationException(); }
/** * Not supported. Always throws UnsupportedOperationException. * @throws UnsupportedOperationException always; <tt>add</tt> * is not supported by this iterator. */ publicvoidadd(E e) { thrownewUnsupportedOperationException(); } }