Java Vector removeRange() Method
removeRange() The method is in Java's Vector A protected method provided by the class, used to remove from the Vector all elements within the specified range. This method is defined in the java.util.Vector class and inherited from java.util.AbstractList 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 remove (inclusive) |
| toIndex | int | The index after the last element to remove (exclusive) |
Method Characteristics
- Range Removal: Can remove multiple elements in a contiguous range at once
- Index Boundaries:
fromIndex must be less than or equal to toIndex- Performance Impact: After removing elements, the indexes of all subsequent elements shift forward accordingly
- Protected Method: Can only be called directly in
Vector's subclasses, or via reflection
Usage examples
Example
import
java.util.Vector
;
public
class
CustomVector
<
E
extends
Vector
<
E
{
public
void
removeElementsInRange
(
int
from,
int
to
)
{// Can directly call the parent class's removeRange() method
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 at indexes 1 to 3 (B and C)
vector.
removeElementsInRange
(
1
,
3
)
;
System
.
out
.
println
(
"Vector after removal: "
vector
)
;
// Output: [A, D, E]
}
}
Notes
- Index Out of Bounds: If the specified index is out of range, throws
IndexOutOfBoundsException - Immutable Collection: Cannot be used on immutable collections or
Collections.unmodifiableList()wrapped Vectors - Concurrent Modification: Additional synchronization measures are needed when used in a multithreaded environment
- Alternatives: If you do not need to inherit from Vector, you can use the
subList(fromIndex, toIndex).clear()
Performance considerations
removeRange() method's time complexity is O(n), where:
- n is the size of the Vector
- In the worst case (removing the first element), all remaining elements must be shifted
For frequent insertion and deletion operations, consider using LinkedList 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 general List interface methods. If you need to expose this functionality, you can wrap it in a subclass.
2. How can range removal be implemented without inheriting from Vector?
You can use the following alternative:
Example
Vector
<
String
vector
=
new
Vector
<>
(
);
// 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