■ Enumerations are used for Vector (which is a List) and Stack (also a List).
■ You must always explicitly cast all returned elements.
■ You cannot remove elements with Enumerations (unlike with the newer iterators)
■ vh.elements( ) returns an enumerator. i.e. assume a Vector v
Enumeration e =
v.elements( );
while (e.hasMoreElements(
)) {
if ( (ObjectCastHere)
(e.nextElement( ) ) == gotcha) then do something; }
or ... Object element =
(YourObjectCast) e.nextElement( );
■ e.nextElement( ) throws a NoSuchElementException after the end, so you need a try-catch here.
import
java.util.*;
try {
Vector v =
new Vector( );
v.add(
"AA" );
v.add(
"BB" );
v.add(
"CC" );
v.add(
"DD" );
Enumeration e
= v.elements( );
while ( e.hasMoreElements(
) ) {
Object
element = (Object) e.nextElement( );
System.out.println(element);
} //prints =>
AA BB CC DD
}
catch
(Exception e) { }