Java Vector remove() Method
Vector The class is an important part of the Java collections framework and implements a growable object array.remove() The method is Vector One of the core methods provided by the class, used to remove the specified element from the vector.
Vector The class provides multiple overloaded remove() methods, allowing developers to remove elements in different ways:
- Remove an element by index position
- Remove an element by object reference
- Remove all elements
Method Syntax
Vector The class provides the following three remove() method in the component:
// 1. Remove the element at the specified index position
public
E remove
(
int
index
)
// 2. Remove the first occurrence of the specified element
public
boolean
remove
(
Object
o
)
// 3. Remove all elements
public
void
clear
(
)
// Actually a method inherited from AbstractCollection
Method details
1. remove(int index)
Function: Removes the element at the specified position in the vector and shifts all subsequent elements to the left (i.e., their indexes decrease by 1).
Parameters:
index- The index position of the element to remove (starting from 0)
Return Value:
- Returns the removed element
exception:
- If the index is out of range (index < 0 || index >= size()), throws
ArrayIndexOutOfBoundsException
Example Code:
Example
import
java.util.Vector
;
public
class
VectorRemoveExample
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
String
fruits
=
new
Vector
<>
(
)
;
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Cherry"
)
;
System
.
out
.
println
(
"Original vector: "
fruits
)
;
// Remove the element at index 1 (Banana)
String
removed
=
fruits.
remove
(
1
)
;
System
.
out
.
println
(
"Removed element: "
removed
)
;
System
.
out
.
println
(
"Vector after removal: "
fruits
)
;
}
}
Output result:
原始向量: [Apple, Banana, Cherry]
被移除的元素: Banana
移除后的向量: [Apple, Cherry]
2. remove(Object o)
Function: Removes the first occurrence of the specified element in the vector (if present).
Parameters:
o- The element to remove from the vector
Return Value:
- Returns if the vector contains the specified element
true - Otherwise returns
false
Example Code:
Example
import
java.util.Vector
;
public
class
VectorRemoveObjectExample
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Integer
numbers
=
new
Vector
<>
(
)
;
numbers.
add
(
10
)
;
numbers.
add
(
20
)
;
numbers.
add
(
30
)
;
numbers.
add
(
20
)
;
If the list contains multiple identical elements, only the index of the first match is returned
System
.
out
.
println
(
"Original vector: "
numbers
)
;
// Remove the first occurrence of 20
boolean
result
=
numbers.
remove
(
Integer
.
valueOf
(
20
)
)
;
System
.
out
.
println
(
"Removal result: "
result
)
;
System
.
out
.
println
(
"Vector after removal: "
numbers
)
;
}
}
Output result:
原始向量: [10, 20, 30, 20]
移除操作结果: true
移除后的向量: [10, 30, 20]
3. clear()
Function: Removes all elements from the vector, making it an empty vector.
Example Code:
Example
import
java.util.Vector
;
public
class
VectorClearExample
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Double
prices
=
new
Vector
<>
(
)
;
prices.
add
(
19.99
)
;
prices.
add
(
29.99
)
;
prices.
add
(
39.99
)
;
System
.
out
.
println
(
"Original vector: "
prices
)
;
// Clear the vector
prices.
clear
(
)
;
System
.
out
.
println
(
"Vector after clearing: "
prices
)
;
System
.
out
.
println
(
"Vector size: "
prices.
size
(
)
)
;
}
}
Output result:
原始向量: [19.99, 29.99, 39.99]
清空后的向量: []
向量大小: 0
method to compare
| Method | Parameter type | Return Value | exception | Purpose |
|---|---|---|---|---|
| remove(int index) | int | E (the removed element) | ArrayIndexOutOfBoundsException | Remove the element at a specific position by index |
| remove(Object o) | Object | boolean | None | Remove the first matching element by value |
| clear() | None | void | None | Remove all elements |
Usage notes
-
Thread Safety:
Vectoris thread-safe, and all its methods use synchronization. But in a single-threaded environment, this may bring unnecessary performance overhead. -
Performance considerations:
remove(int index)The method's time complexity is O(n) because it needs to shift subsequent elements. Frequent removal operations may affect performance. -
Null Value Handling:
Vectorallows storingnullvalue, you can useremove(null)to removenull. -
faster:
remove(Object o)method usesequals()method to determine whether elements match, so ensure the element'sequals()method is implemented correctly. -
Alternatives: After Java 1.2,
ArrayListis usually used asVector's non-synchronized alternative, unless thread safety is really needed.
Best Practices
-
Bulk Removal: If multiple elements need to be removed, consider using
removeAll(Collection c)method. -
Removal During Iteration: When traversing the
Vectorand removing elements, use theIterator'sremove()method to avoidConcurrentModificationException. -
Capacity Adjustment: After frequent removal operations, you can use the
trimToSize()method to reduce the vector's capacity and save memory. -
Checking Existence: Before removing an element, you can use the
contains(Object o)method to check whether the element exists.
Frequently Asked Questions
Q1: What is the difference between remove(int index) and remove(Object o)?
remove(int index) removes the element by index position, while remove(Object o) removes the first matching element by value. When the Vector contains integers, Java automatically selects the matching method, so pay special attention:
Example
Vector
<
Integer
vec
=
new
Vector
<>
(
)
;
vec.
add
(
1
)
;
vec.
add
(
2
)
;
vec.
remove
(
1
)
;
// Remove the element at index 1 (value 2)
vec.
remove
(
Integer
.
valueOf
(
1
)
)
;
// Remove the element with value 1
Q2: How can I safely remove elements during iteration?
Use Iterator for iteration and removal:
Example
Vector
<
String
vec
=
new
Vector
<>
(
)
;
// Add elements...
Iterator
<
String
it
=
vec.
iterator
(
)
;
while
(
it.
hasNext
(
)
)
{
String
s
=
it.
next
(
)
;
if
(
s.
equals
(
"element to remove"
)
)
{
it.
remove
(
)
;
// Safe removal
}
}
Q3: Does the remove() method affect the Vector's capacity?
No.remove() The method only reduces the Vector 's size, but does not automatically reduce its capacity. If you need to shrink the capacity, you can call trimToSize() method.
Summary
Vector 's remove() method provides flexible ways to remove elements; understanding the usage scenarios and caveats of its different overloads is very important for writing robust Java programs. In most modern Java applications, unless thread safety is needed, ArrayList may be a better choice, but understanding Vector and its methods is still valuable.
Java Vector