This example shows how to get the first and last elements from the Vector in Java. This example also shows how to get the first and last elements using the firstElement, lastElement, and get methods.
How to get the first and last element from the Vector in Java?
There are a couple of ways using which we can get the first element or last element from the vector object.
1. Using the firstElement and lastElement methods
The Vector firstElement
method returns the first element of the vector object.
1 |
public E firstElement() |
Similarly, the Vector lastElement
method returns the last element of this vector object.
1 |
public E lastElement() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Vector; public class JavaVectorGetFirstLastElementsExample { public static void main(String[] args) { Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); /* * To get the first element, use * the firstElement method */ System.out.println("First element: " + vColors.firstElement()); /* * To get the last element, use * the lastElement method */ System.out.println("Last element: " + vColors.lastElement()); } } |
Output
1 2 |
First element: Red Last element: Blue |
Important Note:
The firstElement
and lastElement
methods throw NoSuchElementException exception if the vector is empty. Always make sure to check if the vector is empty or not before calling any of these methods to avoid this exception.
1 2 3 4 5 6 7 8 9 10 11 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); if(!vColors.isEmpty()) System.out.println(vColors.firstElement()); if(!vColors.isEmpty()) System.out.println(vColors.lastElement()); |
Output
1 2 |
Red Blue |
2. Using the get method and element index
Since the Vector is an index-based data structure, we can use the Vector get method along with the index to access first and last vector elements.
The vector index starts from 0 and ends at the vector size – 1 index. So the first element of the vector object is located at index 0 while the last element is located at the vector size – 1 index.
1 2 3 4 5 6 7 8 9 10 11 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); //first element is located at index 0 System.out.println( vColors.get(0) ); //last element is located at index (size - 1) System.out.println( vColors.get( vColors.size() - 1 ) ); |
Output
1 2 |
Red Blue |
The get
method throws ArrayIndexOutOfBoundsException exception if the specified index < 0 or specified index >= vector size.
1 2 3 |
Vector<String> vColors = new Vector<String>(); System.out.println( vColors.get( vColors.size() - 1 ) ); |
Output
1 2 3 |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at java.util.Vector.elementData(Unknown Source) at java.util.Vector.get(Unknown Source) |
Always make sure that the vector is not empty to avoid this exception as given below.
1 2 3 4 5 6 7 8 9 10 11 |
Vector<String> vColors = new Vector<String>(); vColors.add("Red"); vColors.add("Green"); vColors.add("Blue"); if(!vColors.isEmpty()) System.out.println( vColors.get(0) ); if(!vColors.isEmpty()) System.out.println( vColors.get( vColors.size() - 1 ) ); |
Output
1 2 |
Red Blue |
This example is a part of the Vector in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation