Java Vector get() Method Tutorial

In this section, we will learn what the Vector get() method is and how to use it in Java.

What is Java Vector get() Method?

The Java Vector get() method is used to get an element from a vector object based on its index number.

Basically, this method takes an index number and searches the target vector object to see what element was stored in that index and returns that value as a result.

Java Vector get() Method Syntax:

public E get(int index)

get() Method Parameters:

The method takes one argument and that is the index number where we want to get the element from there.

get() Method Return Value:

The return value of this method is the element that was in the index number that we set as the argument of the method.

get() Method Exceptions:

This method might throw one exception:

ArrayIndexOutOfBoundsException: We get this exception if we set an index number that is out of the range of the target vector object. For example, if the vector object has 4 indexes from 0 to 3 and we instead set a value like 5 as the argument of this method, this exception will return instead.

Example: using Vector get() method

import java.util.Vector; 
public class Main {

    public static void main(String[] args){
        
        Vector <String> vector = new Vector<>();
        vector.add("Jack");
        vector.add("Omid");
        vector.add("Ellen");
        vector.add("John");

        System.out.println(vector.get(3));
    }
}

Output:

John

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies