This example shows how to remove the first or last element of the Vector in Java. This example also shows how to remove the first or last element from the vector using various methods.
How to remove the first or the last element from Vector in Java?
How to remove the first element from the vector?
The remove
method of the Vector class removes the element at the specified position of the vector object.
1 |
public E remove(int index) |
This method removes and returns the element at the specified position from the vector object.
The vector elements start from the index so to remove the first element from the vector object, we need to pass 0 as the index in this method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Vector; public class VectorRemoveFirstLastElement { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(40); vNumbers.add(50); /* * To remove the first element of the Vector, use * the remove method and pass index as 0 */ System.out.println("Removed: " + vNumbers.remove(0)); System.out.println("Vector contains: " + vNumbers); } } |
Output
1 2 |
Removed: 10 Vector contains: [20, 30, 40, 50] |
How to remove the last element from the vector?
The vector elements start at index 0 and end at vector size – 1 index. To remove the last element from the vector, we will pass the vector size – 1 index in the remove
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.util.Vector; public class VectorRemoveFirstLastElement { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(40); vNumbers.add(50); /* * To remove the last element of the Vector, use * the remove method and pass index as vector size - 1 */ System.out.println("Removed: " + vNumbers.remove(vNumbers.size() - 1)); System.out.println("Vector contains: " + vNumbers); } } |
Output
1 2 |
Removed: 50 Vector contains: [10, 20, 30, 40] |
The remove
method throws ArrayIndexOutOfBoundsException exception if the specified index is out of the range i.e. if the specified index is less than 0 or the specified index is greater than or equal to the vector size.
1 2 3 |
Vector<Integer> vNumbers = new Vector<Integer>(); System.out.println( vNumbers.remove(0) ); |
Output
1 2 |
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 at java.util.Vector.remove(Unknown Source) |
Here, the vector is empty so the size of the vector is 0. We tried to remove the vector element using index 0 that is out of the range.
In order to avoid getting this exception, always make sure to check if the vector is empty before removing elements using the remove
method and an index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(10); vNumbers.add(20); vNumbers.add(30); vNumbers.add(40); vNumbers.add(50); if(!vNumbers.isEmpty()) System.out.println( vNumbers.remove(0) ); if(!vNumbers.isEmpty()) System.out.println( vNumbers.remove(vNumbers.size()-1) ); System.out.println("Vector contains: " + vNumbers); |
Output
1 2 3 |
10 50 Vector contains: [20, 30, 40] |
This example is a part of the Java Vector Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 Vector Documentation