Name

ListIterator<E>

Synopsis

This interface is an extension of Iterator for use with ordered collections, or lists. It defines methods to iterate forward and backward through a list, to determine the list index of the elements being iterated, and, for mutable lists, to safely insert, delete, and edit elements in the list while the iteration is in progress. For some lists, notably LinkedList, using an iterator to enumerate the list’s elements may be substantially more efficient than looping through the list by index and calling get( ) repeatedly.

Like the Iterator interface, ListIterator has been made generic in Java 5.0. The type variable E represents the type of the elements on the list.

hasNext( ) and next( ) are the most commonly used methods of ListIterator; they iterate forward through the list. See Iterator for details. In addition to these two methods, however, ListIterator also defines hasPrevious( ) and previous( ) that allow you to iterate backward through the list. previous( ) returns the previous element on the list or throws a NoSuchElementException if there is no previous element. hasPrevious( ) returns true if a subsequent call to previous( ) returns an object. nextIndex( ) and previousIndex( ) return the index of the object that would be returned by a subsequent call to next( ) or previous( ). If next( ) or previous( ) throw a NoSuchElementException, nextIndex( ) returns the size of the list, and previousIndex( ) returns -1.

ListIterator defines three ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.