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 code to illustrate clear() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector Vector<String> vt = new Vector<String>(); // Use add() method to add elements into the Vector vt.add("Welcome"); vt.add("To"); vt.add("Geeks"); vt.add("4"); vt.add("Geeks"); // Displaying the Vector System.out.println("Vector: " + vt); // Clearing the Vector using clear() method vt.clear(); // Displaying the final vector after clearing; System.out.println("The final Vector: " + vt); } } |
Vector: [Welcome, To, Geeks, 4, Geeks] The final Vector: []
Example 2:
// Java code to illustrate clear() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector Vector<Integer> vt = new Vector<Integer>(); // Use add() method to add elements into the Queue vt.add(10); vt.add(15); vt.add(30); vt.add(20); vt.add(5); // Displaying the Vector System.out.println("Vector: " + vt); // Clearing the Vector using clear() method vt.clear(); // Displaying the final vector after clearing; System.out.println("The final Vector: " + vt); } } |
Vector: [10, 15, 30, 20, 5] The final Vector: []
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.util.BitSet.clear() in Java
- HashSet clear() Method in Java
- LinkedBlockingDeque clear() method in Java
- LinkedList clear() Method in Java
- TreeSet clear() Method in Java
- PriorityQueue clear() Method in Java
- HashMap clear() Method in Java
- LinkedHashMap clear() Method in Java
- Hashtable clear() Method in Java
- TreeMap clear() Method in Java
- EnumMap clear() Method in Java with Examples
- ArrayDeque clear() Method in Java
- WeakHashMap clear() Method in Java
- IdentityHashMap clear() Method in Java
- ArrayBlockingQueue clear() Method in Java
- LinkedBlockingQueue clear() method in Java
- PriorityBlockingQueue clear() method in Java
- ConcurrentSkipListSet clear() method in Java
- CopyOnWriteArrayList clear() method in Java
- CopyOnWriteArraySet clear() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

