Enumeration Interface Example: Enumeration interface is from java.util package. It was introduced with JDK 1.0. It is used by many data structures especially with legacy classes. The DS of JDK 1.0 are known as legacy classes. They are Stack, Vector, Hashtable and Properties.
Enumeration Interface Example
import java.util.*;
public class EnumerationDemo
{
public static void main(String args[])
{
Vector vect1 = new Vector();
vect1.addElement("Raju");
vect1.addElement("Reddy");
vect1.addElement("Rao");
vect1.addElement("Ratnakar");
Enumeration e = vect1.elements();
while(e.hasMoreElements())
{
Object obj1 = e.nextElement();
System.out.println(obj1);
}
}
}
In the above code, the elements() method of Vector returns an object of Enumeration. Following is the elements() method signature defined in Vector class.
Observe, the elements() method returns an object of Enumeration. The above program gives a warning as program is not designed for generics. But the program executes fine.
How Enumeration works?
The Enumeration interface comes with two methods.
1. public abstract boolean hasMoreElements();
2. public abstract Object nextElement();
The Enumeration object e contains all the elements of the Vector vect1. The Enumeration object e maintains a cursor over the elements. By default, the cursor is placed on the first element. The hasMoreElements() returns true if the cursor points to an element else false. If there exists an element (known by the true value), the control enters the while loop. In the while loop, the nextElement() returns the value (pointed by the cursor) and then pushes to the next element. Again the loop goes back and checks whether an element exists or not (known by the return value of hasMoreElements()) and then enters the loop. The nextElement() does its job. Like this, complete elements of the e object are returned. When cursor points to no element, the hasMoreElements() method returns false and the loop terminates.
The nextElement() method returns an object of Object class. If required, it can be casted to the respective element and used in the program. The following modified while loop, adds "great man" if the name is Rao.
while(e.hasMoreElements())
{
Object obj1 = e.nextElement();
String str = (String) obj1;
if(str.equals("Rao"))
{
System.out.println(str + " is a great man");
}
else
{
System.out.println(str);
}
}
The object obj1 is explicitly casted to String and used for comparison.
We can make an analogy for nextElement() method. It is like read() method of FileInputStream. The read() method reads and returns the byte where the file pointer exists and then pushes the file pointer to the next byte.
Sir,
We can print the elements of DS with help of System.out.println () method so why do we need to use Enumeration interface .
To iterate one by one and do actions on each element separately.
Sir the above program is giving classcastexception can u plz check it once..
By default, a whole number is treated as int data type. The value of b1 * b2 is treated as int. It requires explicit type casting to convert int to byte.