Java.util.ArrayList.removeRange() Method



Description

The java.util.ArrayList.removeRange(int fromIndex, int toIndex) method removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the left and reduces their index.

Declaration

Following is the declaration for java.util.ArrayList.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

This method does not return any value.

Exception

IndexOutOfBoundsException − If fromIndex or toIndex are out of range

Example

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

package com.tutorialspoint;

import java.util.*;

   public class ArrayListDemo extends ArrayList {
      public static void main(String[] args) {

      // create an empty array list
      ArrayListDemo arrlist = new ArrayListDemo();

      // use add() method to add values in the list
      arrlist.add(10);
      arrlist.add(12);
      arrlist.add(31);

      // print the list
      System.out.println("The list:" + arrlist);

      // removing range of 1st 2 elements
      arrlist.removeRange(0,2);
      System.out.println("The list after using removeRange:" + arrlist);
   }
}

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

The list:[10, 12, 31]
The list after using removeRange:[31]
java_util_arraylist.htm
Advertisements