Java Vector - get() Method
The java.util.Vector.get() method returns the element at specified index of the vector.
Syntax
public E get(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 the element at specified index of 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.get() method returns element at specified index of 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.get(i)); } } }
The output of the above code will be:
MyVector contains: 10 20 30 40 50
❮ Java.util - Vector