Hi...
Ques : What is an Iterator ? What is the difference between java.util.Iterator and java.util.ListIterator ?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Feb 19, 2012, 2:43 AM
The List and Set collections provide iterators, which are objects that allow going over all the elements of a collection in sequence. The
java.util.Iteratorinterface provides for one-way traversal andjava.util.ListIteratorprovides two-way traversal.Iteratoris a replacement for the olderEnumerationclass which was used before collections were added to Java.Creating an Iterator
Iterators are created by calling the iterator() or listIterator() method of a List, Set, or other data collection with iterators.
Iterator Methods
Iterator defines three methods, one of which is optional.
b =it.hasNext()trueif there are more elements for the iterator.obj =it.next()Object, so a downcast was usually required.it.remove()next. Not all collections supportdelete. An UnsupportedOperationException will be thrown if the collection does not support remove().Iterator : Enables you to traverse through a collection in the forward direction only, for obtaining or removing elements ListIterator : extends Iterator, and allows bidirectional traversal of list and also allows the modification of elements.
Thanks