CHAPTER 21: The Package java.util Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

21.1 The Interface java.util.Enumeration

21.1.1 hasMoreElements , 21.1.2 nextElement

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 hasMoreElements

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 nextElement

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());
}


Top© 1996 Sun Microsystems, Inc. All rights reserved.