Iterators


An iterator is an object that has methods that allow you to proccess a collection of items one at a time.

The java.util.Iterator interface provides the following methods:

The java.util.Scanner class is an example of a class that implements the Iterator interface. The following piece of code reads the text of a file one word at a time.

java.util.Scanner s = new java.util.Scanner(new java.io.File("test.txt"));
while(s.hasNext()) { 
	System.out.println(s.next());
}

Notice that I have used the fully qualified names for the Scanner and File. This obviates the need to import java.util and java.io at the top of the class.

Following is an example of how one might implement an ArrayIterator.

public class ArrayIterator implements java.util.Iterator {    	
	private int current;    
	private Object[] items;    

	public ArrayIterator(Object[] items) {        
		this.current = 0;        
		this.items = items;    
	}    

	public boolean hasNext() {        
		return (current < items.length);    
	}    

	public  Object next() {        
		return items[current++];    
	}    

	public void remove() {        
		throw new UnsupportedOperationException();    
	}        

	public static void main(String[] args) {        
		String[] strings = {"Bob", "Ann", "Sally", "Sue"};        	
		Integer[] ints = {1, 2, 3, 4, 5};        
		ArrayIterator ai = new ArrayIterator(strings);        
		while(ai.hasNext()) {            
			System.out.println(ai.next());        
		}       
		ai = new ArrayIterator(ints);        
		while(ai.hasNext()) {            
			System.out.println(ai.next());        
		}            
	}
}

Notice that this implementation does not support the remove operation. When the remove method is invoked, an UnsupportedOperationException is thrown.


ArrayLists and Iterators

Generics

Java 1.5 supports the concept of generics. To see why generics are convenient, consider the following example that creates an ArrayList of Strings and iterates through the collection printing each value.

import java.util.*;
public class ArrayListIteratorTest {
	public static void main(String[] args) {
		ArrayList al1 = new ArrayList();
		al1.add("one");al1.add("two");
		al1.add("three");
		Iterator i = al1.iterator();
		String l;
		while(i.hasNext()) {
			l = (String)i.next();
			System.out.println(l);
		}
	}
}

Notice that the next method returns an object of type Object that we must cast to String before storing in a String variable. Generics enable the programmer to specify the type of object that will be stored in the ArrayList (or any collection) and Iterator when it is declared and eliminates the need to perform casting.

import java.util.*;
public class ArrayListIteratorTest {
	public static void main(String[] args) {
		ArrayList<String> al1 = new ArrayList<String>();
		al1.add("one");
		al1.add("two");
		al1.add("three");
		Iterator<String> i = al1.iterator();
		String l;
		while(i.hasNext()) {
			l = i.next();
			System.out.println(l);
		}
	}
}

Sami Rollins

Date: 2007-09-05