1 2 4 -1
It is very common for a program to access the elements of a Vector
one by one, in order.
This can be done using a counting loop (as has been done so far this chapter);
however,
an object that implements the Enumeration interface
may also be used.
To get an Enumeration object for a Vector use
this method:
elements() // Returns an enumeration of the components of the vector.
Once you have an enumeration object, the hasMoreElements()
and the nextElement() methods are used to move through the elements:
boolean hasMoreElements() // return true if not all elements have been visited Object nextElement() // Returns the next element of the enumeration.
Here is a program that prints out every element in the Vector:
import java.util.* ;
class VectorEg
{
public static void main ( String[] args)
{
Vector names = new Vector( 10 );
names.addElement( "Amy" ); names.addElement( "Bob" );
names.addElement( "Chris" ); names.addElement( "Deb" );
names.addElement( "Elaine" ); names.addElement( "Frank" );
names.addElement( "Gail" ); names.addElement( "Hal" );
Enumeration enum = names._____________();
while ( enum._____________() )
System.out.println(enum._____________() );
}
}
Unfortunately, the program is full of blanks.