LinkedList source - iterator

LinkedList does not have its own implementation version of the general iterator method (iterator()). Instead, it directl...

LinkedList does not have its own implementation version of the general iterator method (iterator()). Instead, it directly uses the inherited iterator() method:

public Iterator<E> iterator() { return listIterator(); }

The list iterator method is directly called internally, so the focus is on the list iterator.

/** * Returns the list iterator of the elements in this list, starting at the specified location in the list (in the correct order). * Follow {@ code List.listIterator (int)}. * Fast failure * @param index index of the first element to be returned from the * list-iterator (by a call to {@code 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<E> listIterator(int index) { checkPositionIndex(index); return new ListItr(index); }
private class ListItr implements ListIterator<E> { private Node<E> lastReturned;//Property type is Node private Node<E> next; private int nextIndex; private int expectedModCount = modCount; ListItr(int index) { // assert isPositionIndex(index); next = (index == size) ? null : node(index); nextIndex = index; } public boolean hasNext() { return nextIndex < size; } public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } public boolean hasPrevious() { return nextIndex > 0; } public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? last : next.prev; nextIndex--; return lastReturned.item; } public int nextIndex() { return nextIndex; } public int previousIndex() { return nextIndex - 1; } public void remove() { checkForComodification(); if (lastReturned == null) throw new IllegalStateException(); Node<E> lastNext = lastReturned.next; unlink(lastReturned); if (next == lastReturned)// The lastReturned attributes deleted from the linked list are null. The object still exists and still occupies the allocated memory next = lastNext;//Traversal forward else nextIndex--;//Backward traversal lastReturned = null; expectedModCount++; } public void set(E e) { if (lastReturned == null) throw new IllegalStateException(); checkForComodification(); lastReturned.item = e; } public void add(E e) { checkForComodification(); lastReturned = null; if (next == null) linkLast(e); else linkBefore(e, next); nextIndex++; expectedModCount++; } public void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (modCount == expectedModCount && nextIndex < size) { action.accept(next.item); lastReturned = next; next = next.next; nextIndex++; } checkForComodification(); } final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }

The specific logic is also implemented according to the underlying data structure, mainly including several attribute attention types of the iterator, the specific logic to be traversed should be understood, and the modification operation is mainly to call some common methods.

3 June 2020, 10:46 | Views: 5781

Add new comment

For adding a comment, please log in
or create account

0 comments