What does the method elements() do in java?

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 08-Feb-2018 08:58:43
Advertisements