Home »
Java programming language
Java Vector elementAt() Method with Example
Vector Class elementAt() method: Here, we are going to learn about the elementAt() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 15, 2020
Vector Class elementAt() method
- elementAt() method is available in java.util package.
- elementAt() method is used to return the element at the given indices of this Vector.
- elementAt() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- elementAt() method may throw an exception at the time of returning the element.
ArrayIndexOutOfBoundsException: This exception may throw when the given index is less than 0 or greater than the current size.
Syntax:
public Object elementAt(int index);
Parameter(s):
- int index – represents the position of the returned element.
Return value:
The return type of the method is Object, it returns the object of the given index.
Example:
// Java program to demonstrate the example
// of Object elementAt(int indices) method
// of Vector
import java.util.*;
public class ElementAtOfVector {
public static void main(String[] args) {
// Instantiates a Vector object with
// initial capacity of "10"
Vector < String > v = new Vector < String > (10);
// By using add() method is to add the
// elements in this v
v.add("C");
v.add("C++");
v.add("JAVA");
// Display Vector
System.out.println("v: " + v);
// By using elementAt(1) method is to
// return the element at the given indices
// "1"
Object ele = v.elementAt(1);
// Display returned element at the
// given indices
System.out.println("v.elementAt(1): " + ele);
}
}
Output
v: [C, C++, JAVA]
v.elementAt(1): C++