Java Utility Library

Java Vector - elementAt() Method



The java.util.Vector.elementAt() method returns element at the specified index in the vector.

Syntax

public E elementAt(int index)

Here, E is the type of element maintained by the container.


Parameters

index Specify the index number of the element in the vector.

Return Value

Returns element at the specified index in the vector.

Exception

Throws IndexOutOfBoundsException, if the index is out of range i.e., (index < 0 || index > size()).

Example:

In the example below, the java.util.Vector.elementAt() method is used to get the value of element at the specified index in the vector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(20);
    MyVector.add(30);
    MyVector.add(40);
    MyVector.add(50);

    //printing vector using get() method
    System.out.print("MyVector contains:"); 
    for(int i = 0; i < MyVector.size(); i++) {
      System.out.print(" " + MyVector.elementAt(i));
    }
  }
}

The output of the above code will be:

MyVector contains: 10 20 30 40 50

❮ Java.util - Vector