5.3. Iterating Over an ArrayList

Problem

You need to iterate over a portion of an ArrayList. For example, you have an ArrayList with 30 elements, and you need to iterate from index 0 to index 20.

Solution

Use an ArrayListIterator to iterate through a specified region of an ArrayList. This implementation of Iterator is constructed with a reference to an ArrayList and two optional parameters that specify the start and end of an iteration. This example demonstrates the use of ArrayListIterator to iterate over the 3rd, 4th, and 5th elements of an ArrayList:

String[] strings = new String[] { "A", "B", "C", "D", "E", "F" };
List list = new ArrayList( Arrays.asList( strings ) );

Iterator iterator = new ArrayListIterator( list, 3, 6 );
while( iterator.hasNext( ) ) {
        int index = iterator.nextIndex( );
    String element = (String) iterator.next( );
    System.out.println( "Element at " + index + ": " + element );
}

ArrayListIterator also allows you to obtain the index of an element during an iteration. In the previous example, iterator.nextIndex() returns the index of the element returned by the subsequent call to iterator.next( ). The code above iterates through three elements of the ArrayList, producing the following output:

Element at 3: D
Element at 4: E
Element at 5: F

Discussion

You can construct an ArrayListIterator with up to three arguments. The first argument is the ArrayList to be iterated over. The second optional argument specifies the inclusive start index, and the third optional argument specifies the exclusive end index. If only two parameters are supplied to the constructor, the ArrayListIterator will iterate until the end of the ArrayList is reached. The following code demonstrates the three constructors of ArrayListIterator:

String[] strings = new String[] { "A", "B", "C", "D", "E", "F" };
List list = new ArrayList( Arrays.asList( strings ) );

// Iterate over all elements
Iterator iterator1 = new ArrayListIterator( list );

// Iterate over "C", "D", "E", "F"
Iterator iterator2 = new ArrayListIterator( list, 2 );

// Iterate over "B", "C", "D"
Iterator iterator3 = new ArrayListIterator( list, 1, 4 );

ArrayListIterator implements the ResettableIterator interface, which provides one function: reset( ). The reset() method takes the iterator back to the beginning of an iteration. After a reset, a call to next( ) returns the first element that an ArrayListIterator has been configured to return—the element at the index specified in the constructor’s optional second parameter. An ArrayListIterator also provides a way to set the current element: the set( ) method takes an object parameter and changes the contents of the underlying ArrayList. The following example demonstrates both the reset( ) and set() methods on ArrayListIterator:

String[] strings = new String[] { "A", "B", "C", "D", "E", "F" };
List list = new ArrayList( Arrays.asList( strings ) );

System.out.println( "Original List: " + Arrays.toString( list.toArray( ) );

ResettableIterator iterator = new ArrayListIterator( list, 2 );

// Retrieve an Element from the List
int index = iterator.nextIndex( );
String element = (String) iterator.next( );
System.out.println( "Element at " + index + ": " + element );

// Set the Current Element
iterator.set( "G" );
System.out.println( "Modifying index: " + index + " to G");

// Retrieve the Next Element from the List
index = iterator.nextIndex( );
element = (String) iterator.next( );
System.out.println( "Element at " + index + ": " + element );

// Set the Current Element
iterator.set( "H" );
System.out.println( "Modifying index: " + index + " to H");

// Reset the Iterator (Go to beginning of iteration)
iterator.reset( );
System.out.println( "Reset" );

index = iterator.nextIndex( );
element = (String) iterator.next( );
System.out.println( "Element at " + index + ": " + element );

System.out.println( "Modified List: " + Arrays.toString( list.toArray( ) );

This example iterates through the ArrayList, modifying two elements before calling reset( ). The following output shows that after a reset( ), the first element returns the value, which was supplied to set( ):

Original List: {A,B,C,D,E,F}
Element at 2: C
Modifying index: 2 to G
Element at 3: D
Modifying index: 3 to H
Reset
Element at 2: G
Modified List: {A,B,G,H,E,F}

See Also

If you need to iterate over an array or an object array, use two related implementations of Iterator: ArrayIterator and ObjectArrayIterator. See the Commons Collections project page at http://jakarta.apache.org/commons/collections for more information about these Iterator implementations.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.167