Java Fundamental Classes Reference

Previous Chapter 5 Next
 

5. Collections

Contents:
Enumerations
Vectors
Stacks
Hashtables

Java provides a number of utility classes that help you to manage a collection of objects. These collection classes allow you to work with objects without regard to their types, so they can be extremely useful for managing objects at a high level of abstraction. This chapter describes the following collection classes:

5.1 Enumerations

The Enumeration interface is implemented by classes that provide serial access to a set of elements, or objects, in a collection. An object that implements the Enumeration interface provides two methods for dealing with the set: nextElement() and hasMoreElements(). The nextElement() method returns a value of type Object, so it can be used with any kind of collection. When you remove an object from an Enumeration, you may need to cast the object to the appropriate type before using it. You can iterate through the elements in an Enumeration only once; there is no way to reset it to the beginning or move backwards through the elements.

Here is an example that prints the contents of an object the implements the Enumeration interface:

static void printEnumeration(Enumeration e) {
    while (e.hasMoreElements()) {
        System.out.println(e.nextElement());
    }

Note that the above method is able to print all of the objects in the Enumeration without knowing their class types because the println() method handles objects of any type.

A number of classes in the Java API provide a method that returns a reference to an Enumeration object, rather than implementing the Enumeration interface directly. For example, as you'll see shortly, the Vector class provides an elements() method that returns an Enumeration of the objects in a Vector object.


Previous Home Next
Generating Exceptions Book Index Vectors

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java
This HTML Help has been published using the chm2web software.