The java.util.vector.elements() method of Vector class in Java is used to get the enumeration of the values present in the Vector.
Syntax:
Enumeration enu = Vector.elements()
Parameters: The method does not take any parameters.
Return value: The method returns an enumeration of the values of the Vector.
Below programs are used to illustrate the working of the java.util.Vector.elements() method:
Program 1:
Java
import java.util.*;
public class Vector_Demo {
public static void main(String[] args)
{
Vector<String> vec_tor = new Vector<String>( 5 );
vec_tor.add("Geeks");
vec_tor.add(" 4 ");
vec_tor.add("Geeks");
vec_tor.add("Welcomes");
vec_tor.add("You");
System.out.println("The Vector is: " + vec_tor);
Enumeration enu = vec_tor.elements();
System.out.println("The enumeration of values are:");
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:The Vector is: [Geeks, 4, Geeks, Welcomes, You]
The enumeration of values are:
Geeks
4
Geeks
Welcomes
You
Program 2 :
Java
import java.util.*;
public class Vector_Demo {
public static void main(String[] args)
{
Vector<Integer> vec_tor = new Vector<Integer>( 5 );
vec_tor.add( 10 );
vec_tor.add( 15 );
vec_tor.add( 20 );
vec_tor.add( 25 );
vec_tor.add( 30 );
System.out.println("The Vector is: " + vec_tor);
Enumeration enu = vec_tor.elements();
System.out.println("The enumeration of values are:");
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}
|
Output:The Vector is: [10, 15, 20, 25, 30]
The enumeration of values are:
10
15
20
25
30
Time complexity: O(n), // n is the number of elements in the vector.
Auxiliary space: O(n)