Java Reference
In-Depth Information
1 import java.util.ArrayList;
2 import java.util.ListIterator;
3
4 class TestArrayList
5 {
6 public static void main( String [ ] args )
7 {
8 ArrayList<Integer> lst = new ArrayList<Integer>( );
9 lst.add( 2 ); lst.add( 4 );
10 ListIterator<Integer> itr1 = lst.listIterator( 0 );
11 ListIterator<Integer> itr2 = lst.listIterator( lst.size( ) );
12
13 System.out.print( "Forward: " );
14 while( itr1.hasNext( ) )
15 System.out.print( itr1.next( ) + " " );
16 System.out.println( );
17
18 System.out.print( "Backward: " );
19 while( itr1.hasPrevious( ) )
20 System.out.print( itr1.previous( ) + " " );
21 System.out.println( );
22
23 System.out.print( "Backward: " );
24 while( itr2.hasPrevious( ) )
25 System.out.print( itr2.previous( ) + " " );
26 System.out.println( );
27
28
System.out.print( "Forward: ");
for( Integer x : lst )
29
System.out.print( x + " " );
30
System.out.println( );
31
32 }
33 }
figure 6.18
A sample program that illustrates bidirectional iteration
object returned as a result of calling either next or previous , and remove can
only be called once between calls to either next or previous . To override the
javadoc output that is generated for remove , remove is listed in the
ListIterator interface.
The interface in Figure 6.17 is only a partial interface. There are some
additional methods in the ListIterator that we do not discuss in the text, but
which are used throughout as exercises. These methods include add and set ,
which allow the user to make changes to the List at the current location held
by the iterator.
 
Search WWH ::




Custom Search