From charlesreid1

Revision as of 07:02, 19 June 2017 by Admin (talk | contribs)

Also see Java/Iterable#Iterable vs Iterator

Iterator is a wrapper class - think of it as a lightweight scanner object for an iterable data container.

The List interface class (javadocs link) and Linked List class (javadocs link) both utilize a very handy ListIterator class (javadocs link) to iterate over items in a list. The ListIterator class extends the Iterator class, adding a few additional methods - add, hasPrevious, previous, hasNext, next, etc.

To get a ListIterator to a list, use listIterator() method.

To get a ListIterator to a list pointing to index i, use the listIterator(int i) method.

Note that Java's Collections linked list is a doubly linked list, so the previous method is O(1), not O(N).