University of Cincinnati logo and link  
Data Structures
 

 

List Iterator

 
ListIterator listIterator(int index) gives you an iterator starting at the position you specify.

You can move the iterator to the next node with the next() method of ListIterator.
What if you attempt to call next() when no more nodes exist?  Does it throw an ArrayIndexOutOfBounds exception?  No, but close.  It throws a NoSuchElementException.  Before calling next(), you should call hasNext().  hasNext() returns true if there is at least one more element after the current position.  Otherwise, it returns false.
    • How could you use this with a loop?

Stacks and Queues