Open In App
Related Articles

Vector elementAt() Method in Java

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Syntax:

Vector.elementAt(int pos)

Parameters: This method accepts a mandatory parameter pos of integer data type that 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 pos

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

Program 1: 

Java




// Java code to illustrate elementAt() 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("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.elementAt(3));
    }
}


Output:

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

Program 2: 

Java




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


Output:

Vector: [1, 2, 3, 4, 5]
The element is: 2

Time complexity: O(1),
Auxiliary space: O(n).  // n is the size of the vector.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials