Home » Java programming language

Java Vector removeRange() Method with Example

Vector Class removeRange() method: Here, we are going to learn about the removeRange() method of Vector Class with its syntax and example.
Submitted by Preeti Jain, on March 19, 2020

Vector Class removeRange() method

  • removeRange() method is available in java.util package.
  • removeRange() method is used to remove all the elements lies in between starting index (st_index) and ending index (en_index) and st_index is inclusive whereas en_index is exclusive.
  • removeRange() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • removeRange() method does not throw an exception at the time of removing elements in a range.

Syntax:

    public void removeRange(int st_index , int en_index);

Parameter(s):

  • int st_index – represents the starting endpoint to remove.
  • int en_index – represents the ending endpoint to remove.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void removeRange(int st_index , int en_index)
// method of Vector 

import java.util.*;

public class RemoveRangeAtOfVector {
 public static void main(String[] args) {
  // Instantiates a Vector object  with
  // initial capacity of "10"
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add the
  // elements in this v
  v.add("C");
  v.add("C++");
  v.add("JAVA");
  v.add("PHP");
  v.add("SFDC");
  v.add("ANDROID");

  // Display Vector 
  System.out.println("v: " + v);

  // By using removeRange(3,5) method is
  // to remove the element in the given range
  // and removing element starts at index 3 
  // and ends at index 5 but 5th index is exclusive
  // its a protected method so we are using subList instead
  v.subList(3, 5).clear();

  // Display updated vector
  System.out.println("v.removeRange(3,5): " + v);
 }
}

Output

v: [C, C++, JAVA, PHP, SFDC, ANDROID]
v.removeRange(3,5): [C, C++, JAVA, ANDROID]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.