List.listIterator(int).
- *
- * The list-iterator is fail-fast: if the list is structurally
- * modified at any time after the Iterator is created, in any way except
- * through the list-iterator's own remove or add
- * methods, the list-iterator will throw a
- * ConcurrentModificationException. Thus, in the face of
- * concurrent modification, the iterator fails quickly and cleanly, rather
- * than risking arbitrary, non-deterministic behavior at an undetermined
- * time in the future.
- *
- * @param index index of the first element to be returned from the
- * list-iterator (by a call to next)
- * @return a ListIterator of the elements in this list (in proper
- * sequence), starting at the specified position in the list
- * @throws IndexOutOfBoundsException {@inheritDoc}
- * @see List#listIterator(int)
- */
- public ListIterator listIterator(int index) {
- if (index < 0 || index > size)
- throw new IndexOutOfBoundsException("Index: "+index);
- return new ArrayListIterator(index);
- }
-
- /**
- * Returns an iterator over the elements in this list in proper sequence.
- *
- * @return an iterator over the elements in this list in proper sequence
- */
- public Iterator iterator() {
- return new ArrayListIterator(0);
- }
-
- /**
- * A streamlined version of AbstractList.Itr
- */
- final class ArrayListIterator implements ListIterator {
- int cursor; // index of next element to return;
- int lastRet; // index of last element, or -1 if no such
- int expectedModCount; // to check for CME
-
- ArrayListIterator(int index) {
- cursor = index;
- lastRet = -1;
- expectedModCount = modCount;
- }
-
- public boolean hasNext() {
- return cursor < size;
- }
-
- public boolean hasPrevious() {
- return cursor > 0;
- }
-
- public int nextIndex() {
- return cursor;
- }
-
- public int previousIndex() {
- return cursor - 1;
- }
-
- public E next() {
- if (expectedModCount == modCount) {
- int i = cursor;
- if (i < size) {
- try {
- E e = (E)elementData[i];
- lastRet = i;
- cursor = i + 1;
- return e;
- } catch (IndexOutOfBoundsException fallthrough) {
- }
- }
- }
- // Prefer reporting CME if applicable on failures
- if (expectedModCount == modCount)
- throw new NoSuchElementException();
- throw new ConcurrentModificationException();
- }
-
- public E previous() {
- if (expectedModCount == modCount) {
- int i = cursor - 1;
- if (i < size) {
- try {
- E e = (E)elementData[i];
- lastRet = i;
- cursor = i;
- return e;
- } catch (IndexOutOfBoundsException fallthrough) {
- }
- }
- }
- if (expectedModCount == modCount)
- throw new NoSuchElementException();
- throw new ConcurrentModificationException();
- }
-
- public void remove() {
- if (lastRet < 0)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- ArrayList.this.remove(lastRet);
- if (lastRet < cursor)
- cursor--;
- lastRet = -1;
- expectedModCount = modCount;
- }
-
- public void set(E e) {
- if (lastRet < 0)
- throw new IllegalStateException();
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- ArrayList.this.set(lastRet, e);
- expectedModCount = modCount;
- }
-
- public void add(E e) {
- if (modCount != expectedModCount)
- throw new ConcurrentModificationException();
- ArrayList.this.add(cursor++, e);
- lastRet = -1;
- expectedModCount = modCount;
- }
- }
-
}