The Wayback Machine - https://web.archive.org/web/20240618020249/https://www.geeksforgeeks.org/vector-elements-method-in-java/
Open In App

Vector elements() Method in Java

Last Updated : 24 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java code to illustrate the elements() method
import java.util.*;
 
public class Vector_Demo {
    public static void main(String[] args)
    {
 
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>(5);
 
        // Inserting elements into the table
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
        vec_tor.add("Welcomes");
        vec_tor.add("You");
 
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating an empty enumeration to store
        Enumeration enu = vec_tor.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        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)
    {
 
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>(5);
 
        // Inserting elements into the table
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(20);
        vec_tor.add(25);
        vec_tor.add(30);
 
        // Displaying the Vector
        System.out.println("The Vector is: " + vec_tor);
 
        // Creating an empty enumeration to store
        Enumeration enu = vec_tor.elements();
 
        System.out.println("The enumeration of values are:");
 
        // Displaying the Enumeration
        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)



Previous Article
Next Article

Similar Reads

Copy Elements of One Java Vector to Another Vector in Java
Vector is similar to arrays but is growable also, or we can say no fixed size is required. Previously vector was a part of legacy classes but now it is part of Collections. It also implements a List interface, so we can use any method of list interface on vectors also. Syntax : Vector&lt;Integer&gt; gfg=new Vector&lt;&gt;(); Ways To copy elements o
3 min read
Java Program to Shuffle Vector Elements
Vectors basically fall in legacy classes but now it is fully compatible with collections. Java has many built-in functions to perform different operations on collections or other data types and one of them is shuffle. To shuffle Vector elements Collections.shuffle() method is used. It shuffle method of the Collections class shuffles the elements of
3 min read
How to Remove Duplicate Elements from the Vector in Java?
Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements. Example: Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]Approach 1: Using LinkedHashSet LinkedHashSet does not accept duplicate e
3 min read
Searching Elements in Vector Using Index in Java
Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index. An element of a Vector can be searched using an index with different methods. Suppose if the element is not present in the Vector then the method will return -1. Method 1: (Using indexOf(
4 min read
Java Program to Swap the Elements of Vector
The swap() method of java.util.Collections class is used to swap the elements at the specified positions in the specified list. If the specified positions are equal, invoking this method leaves the list unchanged. Syntax: public static void swap(List list, int i, int j) Parameters: This method takes the following argument as a Parameter list – The
2 min read
Reverse Order of All Elements of Java Vector
Vector class Implements a dynamic array means it can shrink and expand its size as required just likely having the same operations like that in the arrays. Don't confuse it with ArrayList as there is a thin line between vector and ArrayList, where the vector is synchronized rest the insertion order remains the same in both of them. Vector is presen
4 min read
Iterate Over Vector Elements in Java
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit. We can iterate over vector by the following ways: Simple for-loopEnhanced for-loopIteratorsEnumeration interface Method 1: Simple for-loop The idea is to run a for loop from start till the size of the
4 min read
Replacing All Elements of Java Vector
All the elements of a vector can be replaced by a specific element using java. util. Collections. fill() method.The fill() method of java.util.Collections class is used to replace all of the elements of the specified list with the specified element. Let's consider the following vector: Let's say that we have to replace all the elements by Value 1,
2 min read
Java Program to Copy Elements of ArrayList to Vector
Vector implements List Interface, like ArrayList it also maintains insertion order but it is rarely used in the non-thread environment as it is synchronized, and due to which it gives a poor performance in adding, searching, deleting, and updating of its elements. To copy elements from one collection to others, pass the object of ArrayList to the c
4 min read
How to Get Random Elements From the Vector in Java?
Vector in java is a part of Java's collections framework. Vector is a dynamic array of objects, i.e., the size of the vector can be modified as per the requirement. Vector implements the List interface. It also maintains the insertion order and the elements of the vector can be accessed using their indexes. The vector in java is synchronized. We ca
4 min read