iterators

 

■ Iterators replace the older Enumeration (although you can still use Enumeration).

 

■ Basically Iterator makes two changes to Enumeration:

(1) You can now do more:  remove elements, go backwards, etc.

(2) There are new methods and names

 

■ The Iterator interface provides three basic methods.  ListIterator adds more.

 

 

boolean  .hasNext( )  method

 

■ Replaces Enumeration's  method e.hasMoreElements( ).  Like e.hasMoreElements( ), hasNext( ) returns true if next( ) will actually find an element.  i.e.

 

String[ ] a = {"1", "2", "3", "4", "5"};

ArrayList  al = new  ArrayList (Arrays.asList( a ) );

    for (     Iterator it = al.iterator( );  it.hasNext( ); )  {

                Object element = it.next( );

                System.out.print(element);

    }

                  

Object  .next( )  method

 

■ It returns the next element in the list.  Replaces Enumeration's method nextElement( ).  Like .nextElement( ), it throws a NoSuchElementException

 

See the example just above

 

 

void  .remove( )  method

 

■ This method effectively cancels the (one single) previous call to .next( ) and it puts the pointer back to where it was, also removing the actual item from the iterator.  i.e This code removes the last item iterated and prints the result, which now has no  5  at the end.

 

String[ ] a = {"1", "2", "3", "4", "5"};

ArrayList  al = new  ArrayList (Arrays.asList( a ) );

Iterator it = al.iterator( );

    for (     ; it.hasNext( ); )  {

                Object element = it.next( );

                System.out.print(element);                 }

it.remove( );

System.out.println(al);

 

■ You cannot use non-Iterator methods to modify a List currently being iterated or a ConcurrentModificationException will be thrown. i.e.

 

List al = new ArrayList( );

al.add( "Mickey" );

al.add( "Minnie" );

al.add( "Donald" );

System.out.println(al);

List sl = Collections.synchronizedList( al );

synchronized( sl ) {

Iterator i = sl.iterator( );                                       // Must be in synchronized block

while (i.hasNext( ))

System.out.println( i.next( ) );  }

 

Iterator x = sl.iterator( );                                      // nw start a new iterator

al.add( "Pluto" );                                                   // Exception is caused here by the add during an iteration

while (x.hasNext( )) System.out.println( x.next( ) );

 

 

ListIterator interface

 

■ This Interface extends the Iterator Interface and adds more methods. You can now go backward or forward.

 

 

void  .add( Obj )  method

 

■ Sticks Obj just before current implicit list cursor so the next next( ) is unaffected.  See the snippet just above for examples of add(...).

 

 

int  .nextIndex( )  method 

 

■ Returns the index of the element that would be returned by the next next( ) . Returns the list size if the iterator is at the end. 

 

 

boolean  .hasPrevious( )   method

 

■ The opposite of  .hasNext( ).  It looks backwards in the index instead of forward.  Returns true if the next previous( ) will actually find an element. 

 

 

Object  .previous( )  method

 

■ The opposite of next( ).  It goes backward instead of forward.  The method hasPrevious( ) will tell you if there’s an element there beforehand.

 

 

int  .previousIndex( )  method

 

■ Returns the index of the element that would be returned by the next next( ). Returns -1 if the iterator is at the front already.