java.util.Vector.removeRange() Method

Advertisements


Description

The removeRange(int fromIndex,int toIndex) method is used to remove all elements from this List whose index is between fromIndex, inclusive and toIndex, exclusive.

Declaration

Following is the declaration for java.util.Vector.removeRange() method

protected void removeRange(int fromIndex,int toIndex)

Parameters

  • fromIndex --This is the index of first element to be removed.

  • toIndex --This is the index after last element to be removed

Return Value

NA

Exception

  • NA

Example

The following example shows the usage of java.util.Vector.removeRange() method.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      // create an empty Vector vec with an initial capacity of 7      
      Vector vec = new Vector(7);

      // use add() method to add elements in the vector
      vec.add(1);
      vec.add(2);
      vec.add(3);
      vec.add(4);
      vec.add(5);
      vec.add(6);
      vec.add(7);

      /**
       * Removing range of elements is not directly supported. However, it
       * can be done by using subList and clear methods.
      */
      System.out.println("Remove elements from 2nd to 4th");
      vec.subList(2,4).clear();
      
      // let us print all the elements available in vector
      System.out.println("Numbers after removal :- "); 
      for (Integer number : vec) {         
         System.out.println("Number = " + number);
      }           
   }    
} 

Let us compile and run the above program, this will produce the following result.

Remove elements from 2nd to 4th
Numbers after removal :- 
Number = 1
Number = 2
Number = 5
Number = 6
Number = 7



Advertisements
Advertisements