Java Vector clear() Method
clear() The method is Vector A commonly used method provided by the class, used to remove all elements from the collection.
public void clear()
Method Functionality
clear() The main function of the method is to clear Vector all elements in the, making it an empty collection. After calling this method,Vector 's size() will return 0, but capacity() remains unchanged.
Key Features
- Removes the
Vectorall elements in the - data to the child component via
Vector's size is reset to 0 - does not change
Vectorthe capacity of the - The time complexity of this operation is O(n), where n is the
Vectorsize of the
Method implementation principle
In Java's Vector In the class,clear() the method's implementation is actually done by calling the removeAllElements() method:
Example
public
void
clear
(
)
{
removeAllElements
(
)
;
}
removeAllElements() The internal implementation of the method iterates over the Vector all elements and sets each position to nullwhile resetting the element counter to 0.
Source Code Analysis
Example
public
synchronized
void
removeAllElements
(
)
{
modCount
++;
// Set all elements to null
for
(
int
i
=
0
;
i
<
elementCount
;
i
++
)
elementData
[
i
]
=
null
;
elementCount
=
0
;
// Reset the element counter
}
Usage examples
Basic Usage
Example
import
java.util.Vector
;
public
class
VectorClearExample
{
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"
)
;
System
.
out
.
println
(
"Vector before clearing: "
fruits
)
;
System
.
out
.
println
(
"Size before clearing: "
fruits.
size
(
)
)
;
// Clear the Vector using the clear() method
fruits.
clear
(
)
;
System
.
out
.
println
(
"Vector after clearing: "
fruits
)
;
System
.
out
.
println
(
"Size after clearing: "
fruits.
size
(
)
)
;
}
}
Output result
清空前的 Vector: [Apple, Banana, Orange]
清空前的大小: 3
清空后的 Vector: []
清空后的大小: 0
Relationship with removeAllElements()
Example
Vector
<
Integer
numbers
=
new
Vector
<>
(
)
;
numbers.
add
(
1
)
;
numbers.
add
(
2
)
;
numbers.
add
(
3
)
;
// clear() and removeAllElements() have the same effect
numbers.
clear
(
)
;
// Clear the Vector
// Equivalent to
// numbers.removeAllElements();
Notes
Capacity unchanged
clear() The method only clears Vector elements in the, but does not change its capacity. If you also need to reduce the capacity, you can call trimToSize() method in the component:
Example
Vector
<
String
items
=
new
Vector
<>
(
20
)
;
// Initial capacity is 20
items.
add
(
"A"
)
;
items.
add
(
"B"
)
;
items.
add
(
"C"
)
;
items.
clear
(
)
;
// Clear the elements; the capacity is still 20
items.
trimToSize
(
)
;
// Adjust the capacity to the current size (0)
Thread Safety
Vector is thread-safe,clear() the method is also synchronized and can be used safely in a multithreaded environment.
Differences from ArrayList
Although ArrayList also has clear() method, Vector 's clear() is synchronized, while ArrayList 's is not:
Example
// Vector's clear() is synchronized
public
synchronized
void
clear
(
)
{
removeAllElements
(
)
;
}
// ArrayList's clear() is not synchronized
public
void
clear
(
)
{
modCount
++;
for
(
int
i
=
0
;
i
<
size
;
i
++
)
elementData
[
i
]
=
null
;
size
=
0
;
}
Frequently Asked Questions
Q1: Does the clear() method release memory?
clear() The method sets all element references to null, which allows these objects to be reclaimed by the garbage collector (if no other references point to them). However,Vector the internal array itself is not released; only the elements in it are cleared.
Q2: What is the difference between clear() and new Vector()?
clear()Reuse the existingVectorobject, clearing only its elementsnew Vector()Create a brand newVectorobject; the oldVectorwill be garbage collected if there are no other references
Q3: How do I determine whether a Vector is empty?
You can use the isEmpty() method in the component:
Example
Vector
<
String
v
=
new
Vector
<>
(
)
;
v.
clear
(
)
;
if
(
v.
isEmpty
(
)
)
{
System
.
out
.
println
(
"The Vector is empty"
)
;
}
Summary
Vector 's clear() The method is a simple but practical tool for quickly clearing all elements in a collection. Understanding how it works and its characteristics helps write more efficient Java code. Remember its characteristics related to capacity and thread safety, as well as best practices in specific scenarios.
Java Vector