Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Vector removeElementAt() Method

Java Vector removeElementAt() Method

Java Vector Java Vector


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:


Method Characteristics

  1. Synchronization: Like most Vector methods,removeElementAt() is thread-safe
  2. Element Shifting: After removing an element, all subsequent elements shift forward by one position
  3. Size Change: The vector's size() decreases by 1
  4. 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

  1. Time Complexity: O(n) on average, because all elements after the removed element must be shifted
  2. Space complexity: O(1), no extra storage space needed
  3. Best Practices:
  4. If elements need to be removed frequently, consider using LinkedList
  5. If you know the element to remove but not its index, you can use removeElement(Object obj) Method
  6. - If elements need to be removed frequently, consider using `LinkedList` - If you know the element to remove but not its index, you can use the `removeElement(Object obj)` method

    Comparison 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

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

    1. Modifying during iteration Vector may cause ConcurrentModificationException
    2. For large Vector; frequent calls to this method may affect performance
    3. 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 Java Vector