Enumeration Interface:
It provides the methods to get a series of elements from a collection of object taking one at a time.
Commonly used methods of Enumeration interface:
1. hasMoreElements(): It returns true if more elements are left to iterate otherwise returns false.
Syntax: public boolean hasMoreElements()
2. nextElement(): It returns the next object of the enumeration.
Syntax: public Object nextElement()
A simple example of Enumeration interface:
EnumerationTest.java
import java.util.Enumeration; import java.util.Vector; /** * This class is used to show the Enumeration functionality. * @author w3schools */ public class EnumerationTest{ public static void main(String args[]){ Enumeration days; Vector weekDays = new Vector(); weekDays.add("Sunday"); weekDays.add("Monday"); weekDays.add("Tuesday"); weekDays.add("Wednesday"); weekDays.add("Thursday"); weekDays.add("Friday"); weekDays.add("Saturday"); days = weekDays.elements(); //Iterate the Enumeration object. while (days.hasMoreElements()){ System.out.println(days.nextElement()); } } } |
Output:
Sunday Monday Tuesday Wednesday Thursday Friday Saturday |
Download this example.
Next Topic: Collection classes in java with example.
Previous Topic: Deque interface in java with example.