21.1 The Interface java.util.Enumeration

An object that implements the Enumeration interface will generate a series of elements, one at a time. Successive calls to the nextElement method will return successive elements of the series.

public interface Enumeration {
    public boolean hasMoreElements();
    public Object nextElement() throws NoSuchElementException;
}

21.1.1 public boolean hasMoreElements()

The result is true if and only if this enumeration object has at least one more element to provide.

21.1.2 public Object nextElement()throws NoSuchElementException

If this enumeration object has at least one more element to provide, such an element is returned; otherwise, a NoSuchElementException is thrown.

As an example, the following code prints every key in the hashtable ht and its length. The method keys returns an enumeration that will deliver all the keys, and we suppose that the keys are, in this case, known to be strings:

Enumeration e = ht.keys();
while (e.hasMoreElements()) {
    String key = (String)e.nextElement();
    System.out.println(key + " " + key.length());
}