How to enumerate through the Vector using Enumeration?

How to enumerate through the Vector using Enumeration?

Previous Home Next

 

This example shows how to enumerate through the Vector using enumeration.

Here we first populate the vector class and the use enumeration to get the elements .
 enumeration provide two methods to enumerate.hasnext() returns true if 
more elements in the vector and next() returns the next element of the 
vector


 

package Example;
import java .util.Enumeration;
import java . util.Vector;
public class EnumExample {

/**
* @param args
*/
public static void main(String[] args) {
// create a vector object
Vector vr=new Vector();
vr.add("one");
vr.add("two");
vr.add("three");
vr.add("four");
Enumeration e=vr.elements();
System.out.println("The elements are:");
while(e.hasMoreElements())
System.out.println(e.nextElement());
// TODO Auto-generated method stub

}

}

The elements are:
one
two
three
four

Previous Home Next