Enumeration and Iteration


You should be familiar with the JDK documentation for class java.util.Vector and interface java.util.Enumeration before you read this document.


Assume you have an object of class java.util.Vector and you have added several objects to it using class Vector's addElement() method. That is, perhaps you have executed some code that looks something like the following:

  Vector v = new Vector();
  <other code>
    v.addElement( ... );
Now assume that you would like to do something with each of the objects in the Vector. You have two choices for getting each of the objects, enumeration and iteration. How do they work, and which one is better?


Class Vector has a method named elements() that returns an Enumeration. Stated more carefully, elements() returns a reference to an object that implements the java.util.Enumeration interface. That means you can use the methods hasMoreElements() and nextElement() to "enumerate over" the objects in the Vector, as follows:

  Enumeration e = v.elements();
  while ( e.hasMoreElements() ) {
    Object o = e.nextElement();
    // Do whatever it is you want to do with o.
The first time through the loop, o will be a reference to the first object in the Vector, and successive times through the loop will give you references to the succeeding objects in the Vector.


You could achieve exactly the same thing by "iterating" over the objects in the Vector, as follows:

  for ( int i = 0; i < v.size(); i++ ) {
    Object o = v.elementAt( i );
    // Do whatever it is you want to do with o.

Comparison of the Two

Most people find the iteration code easier to understand. (There is less of it to deal with, for one thing.) The iteration code is also somewhat more efficient because there is one less object being created (the object that implements the Enumeration interface).

So why bother with the enumeration technique at all? There are basically two answers:

  1. Enumeration is a standard programming pattern that you should be familiar with. Other modern languages besides Java (such as C++) incorporate enumeration either in the language itself or through standard library facilities.

  2. You can't always use iteration. Class Vector is a special case because it supports both iteration and enumeration. Iteration works here because the elements in a Vector really are kept in an array, which means that elementAt() can easily use indexing (subscripting) to get to the objects in the Vector. But other classes that hold collections of objects do not keep them in an array internally, making indexing impossible to do. Hash Tables and Linked Lists are the names of two other ways of storing collections of objects without using a simple array, for example. For these other classes, you have no choice but to enumerate over their contents instead of iterating.

So, become comfortable with using the Enumeration interface, and practice implementing it when you develop classes that hold collections of objects. Enumeration is an idiom of the Java language, and being comfortable with this idiom will help your classes "talk" with other programmers' classes.