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

Vector get() Method in Java

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

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)



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 Get the Maximum Element From a Vector
Prerequisite: Vectors in Java Why We Use Vector? Till now, we have learned two ways for declaring either with a fixed size of array or size enter as per the demand of the user according to which array is allocated in memory. int Array_name[Fixed_size] ; int array_name[variable_size] ; Both ways we land up wasting memory so in order to properly util
4 min read
How to Get First and Last Element From the Vector in Java?
The first element in the Vector can be obtained using the firstElement() method of vector class that is represent util package. The last element in the Vector can be obtained using the lastElement() method of vector class that is also represent util package. Neither of these methods requires any parameters. The first and last elements of the vector
2 min read
Get Enumeration over Java Vector
In java, the vector is a typical dynamic array whose size can increase or decrease. While in array the size cannot be changed after declaration. We have to include file import java.util.Vector to use Vector and store values in it. Also, import java.util.Enumeration to use enumeration. Approach 1: Collection In the Java Enumeration class, all the li
3 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
Get Credential Information From the URL(GET Method) in Java
Given a URL as a string, the task is to extract username, password, profile, role, and key from the URL in a GET Method. Examples: Input: http://www.geeksforgeeks.com/signin/service?username=test&amp;pwd=test&amp;profile=developer&amp;role=ELITE&amp;key=managerOutput:username: testpwd: testprofile: developerrole: ELITEkey: manager Input: http://www
1 min read
Vector clear() Method in Java
The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax: Vector.clear() Parameters: The method does not take any parame
2 min read
Vector addAll() Method in Java
java.util.Vector.addAll(Collection C): This method is used to append all of the elements from the collection passed as a parameter to this function to the end of a vector keeping in mind the order of return by the collection's iterator. Syntax: boolean addAll(Collection C) Parameters: The method accepts a mandatory parameter C which is a collection
2 min read
Vector set() Method in Java
The Java.util.Vector.set() method is used to replace any particular element in the vector, created using the Vector class, with another element. Syntax: Vector.set(int index, Object element) Parameters: This function accepts two mandatory parameters as shown in the above syntax and described below. index: This is of integer type and refers to the p
2 min read
Vector isEmpty() Method in Java
The Java.util.Vector.isEmpty() method in Java is used to check and verify if a Vector is empty or not. It returns True if the Vector is empty else it returns False. Syntax: Vector.isEmpty() Parameters: This method does not take any parameter. Return Value: This function returns True if the Vectoris empty else it returns False. Below programs illust
2 min read