Java Utility Library

Java Vector - elements() Method



The java.util.Vector.elements() method returns an enumeration of the components of the vector. The returned enumeration object will generate all items in the vector. The first item generated is the item at index 0, then the item at index 1, and so on.

Syntax

public Enumeration<E> elements()

Parameters

No parameter is required.

Return Value

Returns an enumeration of the components of the vector.

Exception

NA.

Example:

In the example below, the java.util.Vector.elements() method returns an enumeration of the components of the vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    
    //creating enumeration from a vector
    Enumeration e = MyVector.elements();

    //print all elements of the enumeration
    System.out.print("The Enumeration e contains: ");
    while(e.hasMoreElements())
      System.out.print(e.nextElement() +" ");
  }
}

The output of the above code will be:

The Enumeration e contains: 10 20 30 

❮ Java.util - Vector