Java Vector elementAt() Method

24 Mar 2025 | 2 min read

The elementAt() method of Java Vector class is used to get the element at the specified index in the vector.

Syntax

Following is the declaration of elementAt() method:

Parameter

DataTypeParameterDescriptionRequired/Optional
intindexIt is an index into this vector.Required

Return

The elementAt() method returns an element at the specified index.

Exceptions

ArrayIndexOutOfBoundsException- This method has thrown an exception if the index is out of range i.e. index < 0 || index >= size().

Compatibility Version

Java 1.2 and above

Example 1

Output:

 Element at index 1 is = 2 Element at index 3 is = 4 

Example 2

Output:

 Element at 0th position = White Element at 2nd position = Black 

Example 3

Output:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 >= 3	at java.base/java.util.Vector.elementAt(Vector.java:496)	at myPackage.VectorElementAtExample3.main(VectorElementAtExample3.java:12) 
 
Next TopicJava Vector