Java Vector removeElementAt() Method
removeElementAt(int index) is a method in Java's Vector A method provided by the class, used to remove the element at the specified index position from the vector. This method changes the vector's size, and all elements after the removed element shift forward by one position.
Syntax Description
public void removeElementAt(int index)
Parameter Description
| Parameter name | Type | Description |
|---|---|---|
| index | int | The index position of the element to remove must satisfy 0 ≤ index < size() |
Return Value
This method has no return value (void type).
Exception handling
When this method is called, it may throw the following exception:
ArrayIndexOutOfBoundsException: If the specified index is out of range (index < 0 || index >= size())
Method Characteristics
- Synchronization: Like most
Vectormethods,removeElementAt()is thread-safe - Element Shifting: After removing an element, all subsequent elements shift forward by one position
- Size Change: The vector's
size()decreases by 1 - Capacity unchanged: The vector's
capacity()does not change due to this method call
Usage examples
Basic Usage Example
Example
import
java.util.Vector
;
public
class
VectorExample
{
public
static
void
main
(
String
[
]
args
)
{
// Create a Vector and add elements
Vector
<
String
fruits
=
new
Vector
<>
(
)
;
fruits.
add
(
"Apple"
)
;
fruits.
add
(
"Banana"
)
;
fruits.
add
(
"Orange"
)
;
fruits.
add
(
"Mango"
)
;
System
.
out
.
println
(
"Original Vector: "
fruits
)
;
// Remove the element at index 1 (Banana)
fruits.
removeElementAt
(
1
)
;
System
.
out
.
println
(
"Vector after removal: "
fruits
)
;
}
}
Output result:
原始 Vector: [Apple, Banana, Orange, Mango]
移除后的 Vector: [Apple, Orange, Mango]
Exception Handling Example
Example
import
java.util.Vector
;
public
class
VectorExceptionExample
{
public
static
void
main
(
String
[
]
args
)
{
Vector
<
Integer
numbers
=
new
Vector
<>
(
)
;
numbers.
add
(
10
)
;
numbers.
add
(
20
)
;
try
{
// Try to remove a nonexistent index
numbers.
removeElementAt
(
3
)
;
}
catch
(
ArrayIndexOutOfBoundsException
e
)
{
System
.
out
.
println
(
"Error: "
e.
getMessage
(
)
)
;
}
}
}
Output result:
错误: 3 >= 2
Performance considerations
- Time Complexity: O(n) on average, because all elements after the removed element must be shifted
- Space complexity: O(1), no extra storage space needed
- Best Practices:
LinkedListremoveElement(Object obj) MethodComparison with Other Methods
| Method Names | Description | Whether it changes the size | exception |
|---|---|---|---|
removeElementAt(int index) |
Removes the element at the specified index | Is | ArrayIndexOutOfBoundsException |
removeElement(Object obj) |
Removes the first matching element | Is | None |
remove(int index) |
Removes and returns the element at the specified index | Is | ArrayIndexOutOfBoundsException |
removeAllElements() |
Remove all elements | Is | None |
Practical Application Scenarios
- Dynamic Data Management: When you need to dynamically remove specific elements from a collection based on conditions
- Queue processing: Implement simple queue functionality (although
Vectoris not the best choice) - Data Filtering: Remove elements that do not meet the criteria
Example
// Practical example: remove all empty strings
Vector
<
String
data
=
new
Vector
<>
(
)
;
// ... add data ...
for
(
int
i
=
data.
size
(
)
1
;
i
=
0
;
i
--
)
{
if
(
data.
get
(
i
)
.
isEmpty
(
)
)
{
data.
removeElementAt
(
i
)
;
}
}
Notes
- Modifying during iteration
Vectormay causeConcurrentModificationException - For large
Vector; frequent calls to this method may affect performance - When used in a multithreaded environment, still pay attention to the thread safety of compound operations
By understanding and correctly using removeElementAt() method, you can effectively manage Vector elements in the collection.
Java Vector