Java Vector removeElementAt() Method
removeElementAt(int index)is in JavaVectorA method provided by the class, used to remove the element at the specified index from the vector. This method changes the size of the vector, and all elements after the removed element are shifted one position forward.
Syntax Description
public void removeElementAt(int index)
Parameter Description
| Parameter Name | Type | Description |
|---|---|---|
| index | int | The index position of the element to be removed must satisfy 0 ≤ index < size() |
Return Value
This method has no return value (void type).
Exception Handling
When calling this method, the following exception may be thrown:
ArrayIndexOutOfBoundsException: if the specified index is out of range (index < 0 || index >= size())
Method Features
- Synchronization: like most
Vectormethods,removeElementAt()is thread-safe - Element Shifting: After the element is removed, all subsequent elements are shifted one position forward.
- Size Change: The vector's
size()will decrease by 1 - Capacity Unchanged: The vector's
capacity()will not change as a result of 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);
}
}
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:
原始 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 non-existent index
numbers.removeElementAt(3);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
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 non-existent index
numbers.removeElementAt(3);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
错误: 3 >= 2
Performance Considerations
- Time Complexity: On average O(n), because all elements after the removed element need to be moved
- Space Complexity: O(1), no additional storage space is required
- Best Practices:
- If you need to frequently remove elements, consider using
LinkedList - If you know the element to be removed but not its index, you can use
removeElement(Object obj)method
- If you need to frequently remove elements, consider using
Comparison with Other Methods
| Method Name | Description | Whether Size Changes | Exception |
|---|---|---|---|
removeElementAt(int index) |
Removes the element at the specified index | Yes | ArrayIndexOutOfBoundsException |
removeElement(Object obj) |
Removes the first matching element | Yes | None |
remove(int index) |
Removes and returns the element at the specified index | Yes | ArrayIndexOutOfBoundsException |
removeAllElements() |
Removes all elements | Yes | None |
Practical Application Scenarios
- Dynamic Data Management: When you need to dynamically remove specific elements from a collection based on conditions
- Queue Processing: Implement a simple queue functionality (although
Vectoris not the best choice) - Data Filtering: Remove elements that do not meet the criteria
Example
// Practical application 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);
}
}
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, frequently calling this method may affect performance. - When used in a multi-threaded environment, you still need to pay attention to the thread safety of compound operations.
By understanding and correctly usingremoveElementAt()method, you can effectively manageVectorelements in the collection.
Java Vector