Java Vector - firstElement() Method
The java.util.Vector.firstElement() method returns the first element (element at index = 0) of the vector.
Syntax
public E firstElement()
Here, E is the type of element maintained by the container.
Parameters
No parameter is required.
Return Value
Returns the first element of the vector.
Exception
Throws NoSuchElementException, if the vector is empty.
Example:
In the example below, the java.util.Vector.firstElement() method is used to get the value of first element 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.addElement(50); MyVector.addElement(10); MyVector.addElement(300); //checking vector System.out.println("First Element of Vector is: " + MyVector.firstElement()); } }
The output of the above code will be:
First Element of Vector is: 50
❮ Java.util - Vector