Java Iterator on Doubly-linked-list

Try this:

public boolean hasNext() {
  return current != null;
}
public E next() {
    if (!hasNext()) throw new NoSuchElementException();
    E tmp = current.item;
    current = current.next;  // if next is null, hasNext will return false.
    return tmp;
}

Also drop last and index, you dont need them.

Leave a Comment