Java/Iterator: Difference between revisions
From charlesreid1
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
Iterator is a wrapper class - think of it as a lightweight scanner object for an iterable data container. | Iterator is a wrapper class - think of it as a lightweight scanner object for an iterable data container. | ||
The Linked List class ([https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html javadocs link]) | The List interface class ([http://docs.oracle.com/javase/7/docs/api/java/util/List.html javadocs link]) and Linked List class ([https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html javadocs link]) both utilize a very handy ListIterator class ([http://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html 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, use listIterator() method. | ||
To get a ListIterator to a list pointing to index i, use the listIterator(int i) 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). | |||
Revision as of 07:02, 19 June 2017
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).