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

Java Vector remove() Method

Java Vector remove() Method

Java Vector Java Vector


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:

  1. Remove an element by index position
  2. Remove an element by object reference
  3. 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:

Return Value:

exception:

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:

Return Value:

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

  1. Thread Safety:Vector is thread-safe, and all its methods use synchronization. But in a single-threaded environment, this may bring unnecessary performance overhead.

  2. 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.

  3. Null Value Handling:Vector allows storing null value, you can use remove(null) to remove null .

  4. faster:remove(Object o) method uses equals() method to determine whether elements match, so ensure the element's equals() method is implemented correctly.

  5. Alternatives: After Java 1.2,ArrayList is usually used as Vector 's non-synchronized alternative, unless thread safety is really needed.


Best Practices

  1. Bulk Removal: If multiple elements need to be removed, consider using removeAll(Collection c) method.

  2. Removal During Iteration: When traversing the Vector and removing elements, use the Iterator 's remove() method to avoid ConcurrentModificationException.

  3. Capacity Adjustment: After frequent removal operations, you can use the trimToSize() method to reduce the vector's capacity and save memory.

  4. 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 Java Vector