Easy Tutorial
❮ Dir Empty Java Print Rect ❯

Java Enumeration Interface

Java Data Structures


The Enumeration interface defines several methods that allow for the enumeration (retrieval one by one) of elements in a collection of objects.

This traditional interface has been superseded by the Iterator, although Enumeration is not yet deprecated and is still used infrequently in modern code. Nevertheless, it is employed in methods defined by legacy classes such as Vector and Properties, in some API classes, and is widely used in applications. The following table summarizes some of the methods declared by Enumeration:

Number Method Description
1 boolean hasMoreElements() <br>Tests if this enumeration contains more elements.
2 Object nextElement() <br>Returns the next element of this enumeration if at least one element is still available.

Example

The following example demonstrates the use of Enumeration:

Example

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration<String> days;
      Vector<String> dayNames = new Vector<String>();
      dayNames.add("Sunday");
      dayNames.add("Monday");
      dayNames.add("Tuesday");
      dayNames.add("Wednesday");
      dayNames.add("Thursday");
      dayNames.add("Friday");
      dayNames.add("Saturday");
      days = dayNames.elements();
      while (days.hasMoreElements()){
         System.out.println(days.nextElement()); 
      }
   }
}

The above example compiles and runs with the following result:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

Java Data Structures

❮ Dir Empty Java Print Rect ❯