Iterators


You have learned to use the for-each loop to iterate through each element in a collection. This form of the for loop is a new construct, introduced in J2SE 5.0.

Prior to the for-each loop, the preferred way to iterate through a collection was to ask it for a java.util.Iterator object. An Iterator object maintains an internal pointer to an element in the collection. You can ask an iterator to return the next available element in the collection. After returning an element from the collection, an Iterator advances its internal pointer to refer to the next available element. You can also ask an iterator whether or not there are more elements in a collection.

As an example, rewrite the averageGpaForPartTimeStudents method shown earlier in this lesson to use an Iterator object and a for loop instead of a for-each loop:

 double averageGpaForPartTimeStudents() {    double total = 0.0;    int count = 0;    for (Iterator<Student> it = students.iterator();         it.hasNext(); ) {       Student student = it.next();       if (student.isFullTime())          continue;       count++;       total += student.getGpa();    }    if (count == 0) return 0.0;    return total / count; } 

You usually obtain an Iterator by sending the message iterator to the collection. You assign the Iterator object to an Iterator reference that you bind to the type of object stored within the collection. Here, you bind the Iterator to the type Student, which is what the students collection stores.

The Iterator interface defines three methods: hasNext, next, and remove (which is optional and infrequently needed). The hasNext method returns true if there are any elements remaining in the collection that have not yet been returned. The next method returns the next available element from the collection and advances the internal pointer.

The legacy collections Vector and Hashtable use an analogous technique known as enumeration. The solution is virtually the same, only the names are different. Redefine the students instance variable in Session to be a Vector.

 package sis.studentinfo; import java.util.*; abstract public class Session       implements Comparable<Session> {    ...    private Vector<Student> students = new Vector<Student>();    double averageGpaForPartTimeStudents() {       double total = 0.0;       int count = 0;       for (Enumeration<Student> it = students.elements();            it.hasMoreElements(); ) {          Student student = it.nextElement();          if (student.isFullTime())             continue;          count++;          total += student.getGpa();       }       if (count == 0) return 0.0;       return total / count;    } } 

Instead of asking for an Iterator using the iterator method, you ask for an Enumeration by using the elements method. You test whether there are more elements available using hasMoreElements instead of hasNext. You retrieve the next element using nextElement instead of next. Sun introduced the Iterator in response to many complaints about the unnecessary verbosity of the class name Enumeration and its method names.

Since Vector implements the List interface, you can send a Vector the iterator message to obtain an Iterator instead of an Enumeration.

Revert your Session code to use modern collections and the for-each loop.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net