The Java.util.Vector.clear() method is used to remove all the elements from a Vector. Using the clear() method only clears all the element from the vector and does not delete the vector. In other words, we can say that the clear() method is used to only empty an existing vector. Syntax:
Vector.clear()
Parameters: The method does not take any parameter Return Value: The function does not returns any value. Below programs illustrate the Java.util.Vector.clear() method. Example 1:
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
Vector<String> vt = new Vector<String>();
vt.add("Welcome");
vt.add("To");
vt.add("Geeks");
vt.add("4");
vt.add("Geeks");
System.out.println("Vector: " + vt);
vt.clear();
System.out.println("The final Vector: " + vt);
}
}
|
Output:
Vector: [Welcome, To, Geeks, 4, Geeks]
The final Vector: []
Example 2:
Java
import java.util.*;
public class GFG {
public static void main(String args[])
{
Vector<Integer> vt = new Vector<Integer>();
vt.add(10);
vt.add(15);
vt.add(30);
vt.add(20);
vt.add(5);
System.out.println("Vector: " + vt);
vt.clear();
System.out.println("The final Vector: " + vt);
}
}
|
Output:
Vector: [10, 15, 30, 20, 5]
The final Vector: []
Time complexity: O(n). // n is the size of the vector.
Auxiliary Space: O(n).
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
21 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...