java.util.Vector.elements() Method
Advertisements
Description
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.
Declaration
Following is the declaration for java.util.Vector.elements() method
public Enumeration<E> elements()
Parameters
Does not take any input parameter
Return Value
It returns an enumeration of the components of this vector.
Exception
-
NA
Example
The following example shows the usage of java.util.Vector.elements() method.
package com.tutorialspoint;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
// create an empty Vector vec with an initial capacity of 4
Vector<Integer> vec = new Vector<Integer>(4);
// use add() method to add elements in the vector
vec.add(4);
vec.add(3);
vec.add(2);
vec.add(1);
// adding elements into the enumeration
Enumeration e=vec.elements();
// let us print all the elements available in enumeration
System.out.println("Numbers in the enumeration are :- ");
while (e.hasMoreElements()) {
System.out.println("Number = " + e.nextElement());
}
}
}
Let us compile and run the above program, this will produce the following result.
Numbers in the enumeration are :- Number = 4 Number = 3 Number = 2 Number = 1