Java Enumeration

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

Java Enumeration

Posted on: August 2, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java Enumeration  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Padma
May 29, 2013
program

This site is being very useful. If it is possible, please send me more programs on each topic to my mail id.

Ads

 

Ads