Java Vector removeRange() Method
removeRange()The method is aVectorprotected method provided by the class, used toVectorremove all elements within the specified range from it. This method is defined injava.util.Vectorthe class and inherits fromjava.util.AbstractListthe class.
Syntax Description
protected void removeRange(int fromIndex, int toIndex)
Parameter Description
| Parameter Name | Type | Description |
|---|---|---|
| fromIndex | int | The index of the first element to be removed (inclusive) |
| toIndex | int | The index after the last element to be removed (exclusive) |
Method Characteristics
- Range removal: Can remove multiple elements in a contiguous range at once
- Index boundaries:
fromIndexMust be less than or equal totoIndex- Both indices must be within the valid range of the Vector (0 ≤ fromIndex ≤ toIndex ≤ size())
- Performance impact: After removing elements, the indices of all subsequent elements will shift forward accordingly
- Protected method: Can only be
Vectordirectly called in subclasses, or invoked through the reflection mechanism
Usage Example
Example
import java.util.Vector;
public class CustomVector<E> extends Vector<E> {
public void removeElementsInRange(int from, int to) {
// Can directly call the removeRange() method of the parent class
this.removeRange(from, to);
}
public static void main(String[] args) {
CustomVector<String> vector = new CustomVector<>();
// Add elements
vector.add("A");
vector.add("B");
vector.add("C");
vector.add("D");
vector.add("E");
System.out.println("Original Vector: " + vector); // Output: [A, B, C, D, E]
// Remove elements from index 1 to 3 (B and C)
vector.removeElementsInRange(1, 3);
System.out.println("Vector after removal: " + vector); // Output: [A, D, E]
}
}
public class CustomVector<E> extends Vector<E> {
public void removeElementsInRange(int from, int to) {
// Can directly call the removeRange() method of the parent class
this.removeRange(from, to);
}
public static void main(String[] args) {
CustomVector<String> vector = new CustomVector<>();
// Add elements
vector.add("A");
vector.add("B");
vector.add("C");
vector.add("D");
vector.add("E");
System.out.println("Original Vector: " + vector); // Output: [A, B, C, D, E]
// Remove elements from index 1 to 3 (B and C)
vector.removeElementsInRange(1, 3);
System.out.println("Vector after removal: " + vector); // Output: [A, D, E]
}
}
Precautions
- Index out of bounds: If the specified index is out of range, it will throw
IndexOutOfBoundsException - Immutable collections: Cannot be used on immutable collections or a
Collections.unmodifiableList()wrapped Vector - Concurrent modification: Additional synchronization measures are required when used in a multi-threaded environment
- Alternative approach: If you do not need to inherit Vector, you can use
subList(fromIndex, toIndex).clear()
Performance Considerations
removeRange()The time complexity of the method is O(n), where:
- n is the size of the Vector
- In the worst case (removing the leading element), all remaining elements need to be moved
For frequent insertion and deletion operations, consider usingLinkedListwhich may be more efficient.
Common Questions
1. Why is removeRange() a protected method?
This is a design decision of the Java Collections Framework, intended to encourage developers to use the more genericListinterface methods. If you need to expose this functionality, you can wrap it in a subclass.
2. How to implement range removal without inheriting Vector?
The following alternatives can be used:
Example
Vector<String> vector = new Vector<>();
// Add elements...
// Use a combination of subList and clear
vector.subList(1, 3).clear();
// Add elements...
// Use a combination of subList and clear
vector.subList(1, 3).clear();
3. What is the difference between removeRange() and removeAll()?
removeRange()Removes elements within the specified index rangeremoveAll()Removes all elements matching those in the specified collection
Java Vector