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
// Java program to understand // about vector.removeRange() function Â
import java.util.*; Â
// since 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
// Java program to understand // about vector.removeRange() function Â
import java.util.*; Â
// since 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.
Â