❮ Previous Next ❯

JAVA Enumeration Interface

Enumeration is an interface introduced in 1.0V(legacy) used to retrieve data one by one
from Vector only. Using Enumeration object we will do this. There is a method elements()
invoking by Vector object, then we will get Enumeration object.

Enumeration e = v.elements();

Here v is any Vector object.

Enumeration defines the following 2 methods like:

1. public boolean hasMoreElements();

Tests if this enumeration contains more elements. It returns true if and only if this
enumeration object contains at least one more element to provide; otherwise it returns false.

2. public Object nextElement();

Returns the next element of this enumeration if this enumeration object has at least one more
element to provide. It returns the next element of this enumeration. It throws
NoSuchElementException - if no more elements exist.

EnumerationExample.java

import java.util.*;
class EnumerationExample
    {
             public static void main(String[] args)
                {
                Vector v=new Vector();
                for(inti=1;i<=10;i++)
                  {
                     v.addElement(i);
                  }
                System.out.println(v);
                Enumeration e=v.elements();
                while(e.hasMoreElements())
                  {
                     Integer i=(Integer)e.nextElement();
                     if(i%2==0)
                     System.out.println(i);
                  }
                }
    }
Output
[1,2,3,4,5,6,7,8,9,10]
2
4
6
8
10

❮ Previous Next ❯



  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved