Home Java Example Java Util Java Enumeration



Java Enumeration
Posted on: August 2, 2011 at 12:00 AM
In this section you will learn about Enumeration interface and it's implementation.

Java Enumeration

In this section you will learn about Enumeration interface and it's implementation.

Enumerate means get one at a time. As name is telling, the Enumeration interface have functions by which you can obtain one element at a time from the collection of object.

Now a days, Iterator replaces Enumeration. Enumeration is believed as no longer use for new code. But it is still in use where some of the legacy classes such as Vector and Properties is used by several other API classes.

The methods of Enumeration are given below :

Methods  Description
boolean hasMoreElements( )  Return true when there are still element/elements
left for enumerate otherwise false 
Object nextElement( ) Returns the next object in the Enumeration.

EXAMPLE

Given below example uses Enumeration for traversing elements one by one:

import java.util.*;

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

OUTPUT

Sunday

Monday

Tuesday

Wednesday                             

Thursday

Friday

Saturday

Download Source Code

Related Tags for Java Enumeration:


More Tutorials from this section

Ask Questions?    Discuss: Java Enumeration  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.