Java Vector Clear
[ Java Vector](#)
The `clear()` method is a commonly used method provided by the `Vector` class, used to remove all elements from the collection.
public void clear()
* * *
## Method Functionality
The main function of the `clear()` method is to empty all elements in the `Vector`, making it an empty collection. After calling this method, `Vector`'s `size()` will return 0, but `capacity()` remains unchanged.
### Key Features
* Remove all elements from the `Vector`
* Reset the size of the `Vector` to 0
* Does not change the `Vector`'s capacity
* The time complexity of this operation is O(n), where n is the size of the `Vector`
* * *
## Method Implementation Principle
In Java's `Vector` class, the implementation of the `clear()` method is actually accomplished by calling the `removeAllElements()` method:
## Example
public void clear(){
removeAllElements();
}
The internal implementation of the `removeAllElements()` method iterates through all elements in the `Vector`, sets each position to `null`, and resets 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=null;
elementCount =0;// Reset 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 fruits =new Vector();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
System.out.println("Vector before clear: "+ fruits);
System.out.println("Size before clear: "+ fruits.size());
// Use clear() method to empty the Vector
fruits.clear();
System.out.println("Vector after clear: "+ fruits);
System.out.println("Size after clear: "+ fruits.size());
}
}
#### Output Result
Vector before clear: [Apple, Banana, Orange]Size before clear: 3Vector after clear: []Size after clear: 0
### Relationship with removeAllElements()
## Example
Vector numbers =new Vector();
numbers.add(1);
numbers.add(2);
numbers.add(3);
// clear() and removeAllElements() have the same effect
numbers.clear();// Empty the Vector
// Equivalent to
// numbers.removeAllElements();
* * *
## Precautions
### Capacity Unchanged
The `clear()` method only empties the elements in the `Vector` but does not change its capacity. If you need to reduce the capacity as well, you can call the `trimToSize()` method:
## Example
Vector items =new Vector(20);// Initial capacity is 20
items.add("A");
items.add("B");
items.add("C");
items.clear();// Empty elements, capacity remains 20
items.trimToSize();// Adjust capacity to current size (0)
### Thread Safety
`Vector` is thread-safe, and the `clear()` method is also synchronized, making it safe to use in multi-threaded environments.
### Difference from ArrayList
Although `ArrayList` also has a `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=null;
size =0;
}
* * *
## FAQ
### Q1: Does the clear() method release memory?
The `clear()` method sets all element references to `null`, which allows these objects to be garbage collected (if no other references point to them). However, the internal array of the `Vector` itself is not released; only the elements in it are cleared.
### Q2: What is the difference between clear() and new Vector()?
* `clear()` reuses the existing `Vector` object, only clearing its elements
* `new Vector()` creates a brand new `Vector` object, and the old `Vector` will be garbage collected if there are no other references to it
### Q3: How to check if a Vector is empty?
You can use the `isEmpty()` method:
## Example
Vector v =new Vector();
v.clear();
if(v.isEmpty()){
System.out.println("Vector is empty");
}
* * *
## Summary
`Vector`'s `clear()` method is a simple but useful tool for quickly emptying all elements in a collection. Understanding its working principle and features 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](#)
YouTip