Skip to content
Related Articles

Related Articles

Vector removeRange() method in Java with Example
  • Last Updated : 24 Dec, 2018

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);
    }
}
// sice vector removeRange() method is protected
// 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);
        }
    }
}
Output:
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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :