 |
Enumeration
|
Enumeration: Accessing Objects in a Vector
-
Use Enumeration class to traverse collections
-
Vector defines the method:
Enumeration elements ()
which returns an enumeration (an ordering of elements) for a vector.
Enumeration defines the methods:
boolean hasMoreElements () - true if more elements; false otherwise
Object nextElement () - returns next element in an enumeration
which returns an enumeration (an ordering of elements) for a vector.
So, to access all the objects in a Vector:
-
Get an enumeration
-
Ask enumeration if there are elements to process
-
If there are, fetch Object from enumeration - note must cast!
Example:
import cucs.*;
import java.util.*;
...
Vector messages = new Vector ();
BasicDataReader inFile = new BasicDataReader ("mydata.txt");
while (!inFile.eof ())
messages.addElement (inFile.readLine());
...
String tmp;
Enumeration theMessages;
theMessages = messages.elements ();
while (theMessages.hasMoreElements ()) {
tmp = (String) theMessages.nextElement();
System.out.println (tmp);
}
Last modified: 10/20/1999