Saturday, August 30, 2025
HomeLanguagesJavaVector get() Method in Java

Vector get() Method in Java

The java.util.vector.get() method is used to fetch or retrieve an element at a specific index from a Vector. 

Syntax:

Vector.get(int index)

Parameters: This method accepts a mandatory parameter index which is of integer data type. It specifies the position or index of the element to be fetched from the Vector. 

Return Value: The method returns the element present at the position specified by the parameter index. 

Below programs illustrate the Java.util.Vector.get() method: 

Program 1

Java




// Java code to illustrate get() method
import java.util.*;
 
public class VectorDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
 
        // Use add() method to add elements in the Vector
        vec_tor.add("Geeks");
        vec_tor.add("for");
        vec_tor.add("Geeks");
        vec_tor.add("10");
        vec_tor.add("20");
 
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
 
        // Fetching the specific element from the Vector
        System.out.println("The element is: "
                           + vec_tor.get(2));
    }
}


Output:

Vector: [Geeks, for, Geeks, 10, 20]
The element is: Geeks

Program 2

Java




// Java code to illustrate get() method
import java.util.Vector;
 
public class VectorDemo {
    public static void main(String args[])
    {
 
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
 
        // Use add() method to add elements in the Vector
        vec_tor.add("1");
        vec_tor.add("2");
        vec_tor.add("3");
        vec_tor.add("10");
        vec_tor.add("20");
 
        // Displaying the Vector
        System.out.println("Vector: "
                           + vec_tor);
 
        // Fetching the specific element from the Vector
        System.out.println("The element is: "
                           + vec_tor.get(4));
    }
}


Output:

Vector: [1, 2, 3, 10, 20]
The element is: 20

Time complexity: O(n), // n is the number of elements in the vector.
Auxiliary space: O(n)

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11839 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6702 POSTS0 COMMENTS