Latest web development tutorials

Java Enumeration Interface

Enumeration interface defines the methods, these methods can be enumerated (once obtained a) objects in the collection elements.

This tradition has been replaced by the interface iterator, although Enumeration has not been abandoned, but in the modern code has been rarely used. Nevertheless, it is used in the process, such as Vector and Properties of these traditional categories are defined, in addition, also used in a number of API classes, and applications are also widely used. The following table summarizes some of the ways Enumeration declaration:

No. Method Description
1 boolean hasMoreElements ()
Tests if this enumeration contains more elements.
2 Object nextElement ()
If the elements of this enumeration object has at least one offer, then return the next element of this enumeration.

Examples

The following example demonstrates the use of the Enumeration:

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

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration days;
      Vector dayNames = new Vector();
      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 examples compiled results are as follows:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday