Vector removeRange() method in Java with Example
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)
Syntax:
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)
Example 1:
// Java program to understand // about vector.removeRange() function mport java.util.*; // sice vector removeRange() method is protected // Therefore Vector is inherited public class GFG extends Vector<String> { // main method public static void main(String[] args) { // creating GFG object GFG v = new GFG(); // inserting elements into the vector v.add("Geeks"); v.add("for"); v.add("Geeks"); v.add("Ankit"); v.add("Mishra"); v.add("MNNIT"); // printing vector before deleting System.out.println("Vector before calling" + " removeRange(): " + v); // calling removeRange() function v.removeRange(1, 3); // printing after removeRange() called System.out.println("Vector after calling" + " removeRange(1, 3): " + v); } } |
// Therefore Vector is inherited
Output:
Vector before calling removeRange(): [Geeks, for, Geeks, Ankit, Mishra, MNNIT] Vector after calling removeRange(1, 3): [Geeks, Ankit, Mishra, MNNIT]
Example 2:
// Java program to understand // about vector.removeRange() function import java.util.*; // sice vector removeRange() method is protected // Therefore Vector is inherited public class GFG extends Vector<String> { // main method public static void main(String[] args) { // creating GFG object GFG v = new GFG(); // inserting elements into the vector v.add("Geeks"); v.add("for"); v.add("Geeks"); v.add("Ankit"); v.add("Mishra"); v.add("MNNIT"); // printing vector before deleting System.out.println("Vector before calling " + "removeRange(): " + v); // calling removeRange() function try { // It will generate Runtime Error v.removeRange(1, 16); // printing after removeRange() called System.out.println("Vector after " + "calling removeRange(1, 3): " + v); } catch (Exception e) { System.out.println(e); } } } |
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.
Recommended Posts:
- Stack removeRange() method in Java with Example
- Arraylist removeRange() in Java with examples
- Vector get() Method in Java
- Vector set() Method in Java
- Vector add() Method in Java
- Vector contains() Method in Java
- Vector removeElementAt() Method in Java
- Vector lastElement() Method in Java
- Vector copyInto() Method in Java
- Vector ensureCapacity() method in Java with Example
- Vector insertElementAt() Method in Java
- Vector hashCode() Method in Java
- Vector size() Method in Java
- Vector equals() Method in Java
- Vector capacity() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



