Java Vector removeElementAt() Method

Java Vector Java Vector


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

  1. Synchronization: like mostVectormethods,removeElementAt()is thread-safe
  2. Element Shifting: After the element is removed, all subsequent elements are shifted one position forward.
  3. Size Change: The vector'ssize()will decrease by 1
  4. Capacity Unchanged: The vector'scapacity()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);
    }
}

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());
        }
    }
}

Output:

错误: 3 >= 2

Performance Considerations

  1. Time Complexity: On average O(n), because all elements after the removed element need to be moved
  2. Space Complexity: O(1), no additional storage space is required
  3. Best Practices:
    • If you need to frequently remove elements, consider usingLinkedList
    • If you know the element to be removed but not its index, you can useremoveElement(Object obj)method

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

  1. Dynamic Data Management: When you need to dynamically remove specific elements from a collection based on conditions
  2. Queue Processing: Implement a simple queue functionality (althoughVectoris not the best choice)
  3. 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);
    }
}

Notes

  1. Modifying during iterationVectormay causeConcurrentModificationException
  2. For largeVector, frequently calling this method may affect performance.
  3. 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 Java Vector

Other Extensions