83
Java Vector removeElementAt() Method
The removeElementAt() method of Java Vector class is used to delete the component at the specified index. Each element in this vector with an index greater or equal to the given index is shifted downward. Now the vector index is one smaller than the value it had previously.
Syntax
Following is the declaration of removeElementAt() method:
Parameter
DataType | Parameter | Description | Required/Optional |
---|---|---|---|
int | index | It is an index of the object which will be removed.Required |
Return
This method does not return anything. It only removed the element from the vector.
Exceptions
IndexOutOfBoundsException– This method has thrown an exception if the index of an array is out of range i.e. (index < 0 || index >= size()).
Compatibility Version
Java 1.2 and above
Example 1
Output:
Vector element before removal: [1, 2, 3, 4, 5, 6] Vector element after removal: [1, 2, 3, 4, 5]
Example 2
Output:
The elements of a vector before removal: [Rahul, Karan, Rahul, Rohan] The elements of a vector after removal: [Rahul, Karan, Rohan]
Example 3
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 50 >= 3 at java.base/java.util.Vector.removeElementAt(Vector.java:579) at myPackage.VectorRemoveElementAtExample3.main(VectorRemoveElementAtExample3.java:12)
Next TopicJava Vector