Thursday, 20 June 2013

Enumeration Interface in JAVA

The Enumeration interface specifies a set of methods that may be used to enumerate, or count through, a set of values. We can enumerate the elements of collection of objects using Enumeration interface. Enumeration interface is legacy interface.

Following are the methods available in Enumeration interface:


NOTE: nextElement() method throws NoSuchElementException if no more elements exist.

Example:

package com.sample.javase.testing;

import java.util.Enumeration;
import java.util.Vector;

public class EnumerationInterfaceExample {

/**
* @param args
*/
public static void main(String[] args) {

Vector<String> strings = new Vector<String>();
strings.addElement("inheritence");
strings.addElement("polymorphism");
strings.addElement("encapsulation");
Enumeration<String> enumeration = strings.elements();
while (enumeration.hasMoreElements()) {
String string = (String) enumeration.nextElement();
System.out.println(string);

}

}

No comments:

Post a Comment