9. 35. 1. The Enumeration Interface |
|
The Enumeration interface defines a way to traverse all the members of a collection of objects. |
The hasMoreElements() method checks to see if there are more elements and returns a boolean. |
If there are more elements, nextElement() will return the next element as an Object. |
If there are no more elements when nextElement() is called, the runtime NoSuchElementException will be thrown. |
import java.util.Enumeration;
import java.util.Vector;
public class MainClass {
public static void main(String args[]) throws Exception {
Vector v = new Vector();
v.add("a");
v.add("b");
v.add("c");
Enumeration e = v.elements();
while (e.hasMoreElements()) {
Object o = e.nextElement();
System.out.println(o);
}
}
}
|
|
a
b
c |