What does the method elements() do in java?


The elements() method is used to return an enumeration of the components of this vector. The returned Enumeration object will generate all items in this vector at the similar index location.

Example

import java.util.Vector;
public class VectorDemo {
   public static void main(String[] args) {
     
      Vector<Integer> vec = new Vector<Integer>(4);
      vec.add(4);
      vec.add(3);
      vec.add(2);
      vec.add(1);
      Enumeration e=vec.elements();
      System.out.println("Numbers in the enumeration are :- ");
      while (e.hasMoreElements()) {
         System.out.println("Number = " + e.nextElement());
      }
   }
}

Output

Numbers in the enumeration are :-
Number = 4
Number = 3
Number = 2
Number = 1

Updated on: 25-Feb-2020

80 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements