Java Vector get(int index) Example

The elementAt(int index) method of java.util.Vector class will get you the element at the specified index same way get(int index) method also returns the element at the specified index.

Signature

public E get(int index) 

Retrieves the element which is present at the particular index and will throw java.lang.ArrayIndexOutOfBoundsException if the index is not within the range

Example

The following example shows the usage of java.util.Vector.get(index) method.

import java.util.Vector;

public class GetMethodVectorExample 
{
    public static void main(String args[])
    {
        // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element3");
        
        //Getting element at a praticular index
        System.out.println("Element at index 1 is : "+vector1.get(1));
        
    }
}

Output

Element at index 1 is : Element2

Leave a Reply

Your email address will not be published. Required fields are marked *