The removeRange() method of Vector in Java is used to remove all elements within the specified range from an Vector object. It shifts any succeeding elements to the left. This call shortens the Vector by (toIndex-fromIndex) elements where toIndex is the ending index and fromIndex is the starting index within which all elements are to be removed. (If toIndex==fromIndex, this operation has no effect)
removeRange(int fromIndex, int toIndex)
Parameters: There are two parameters :
- fromIndex: starting index from which index elements are to be removed.
- toIndex: within range[fromIndex-toIndex), all elements are removed.
Return Value: This method does not return any value. It only removes all the elements within the specified range.
Exceptions: This method throws IndexOutOfBoundsException if fromIndex or toIndex is out of range (fromIndex = size() or toIndex > size() or toIndex < fromIndex)
// Therefore Vector is inherited
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT] Vector after calling removeRange(1, 3): [Geeks, Ankit, Mishra, MNNIT]
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT] java.lang.ArrayIndexOutOfBoundsException
Note:It is strongly recommended that if we want to call removeRange() method in any class then that class should necessarily extends Vector class either direct or indirect otherwise we will get compilation error : Method removeRange() has protected access.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.