The Java.util.Vector.removeAllElements() method is used to removes all components from this Vector and sets its size to zero.
Syntax:
Vector.removeAllElements()
Parameters: The method does not take any parameter
Return Value: The function does not returns any value.
Below programs illustrate the Java.util.Vector.removeAllElements() method.
Example 1:
// Java code to illustrate removeAllElements() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector Vector<String> vector = new Vector<String>(); // Use add() method to add elements into the Vector vector.add("Welcome"); vector.add("To"); vector.add("Geeks"); vector.add("4"); vector.add("Geeks"); // Displaying the Vector System.out.println("Vector: " + vector); // removeAllElementsing the Vector // using removeAllElements() method vector.removeAllElements(); // Displaying the final Vector after removeAllElement System.out.println("The final Vector: " + vector); } } |
Vector: [Welcome, To, Geeks, 4, Geeks] The final Vector: []
Example 2:
// Java code to illustrate removeAllElements() import java.util.*; public class GFG { public static void main(String args[]) { // Creating an empty Vector Vector<Integer> vector = new Vector<Integer>(); // Use add() method to add elements into the Queue vector.add(10); vector.add(15); vector.add(30); vector.add(20); vector.add(5); // Displaying the Vector System.out.println("Vector: " + vector); // removeAllElementsing the Vector // using removeAllElements() method vector.removeAllElements(); // Displaying the final Vector after removeAllElement System.out.println("The final Vector: " + vector); } } |
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:
- Stack removeAllElements() method in Java with Example
- vector :: cbegin() and vector :: cend() in C++ STL
- Vector removeElement() method in Java with Example
- Vector removeRange() method in Java with Example
- Vector toString() method in Java with Example
- Vector setSize() method in Java with Example
- Vector trimToSize() method in Java with Example
- Vector setElementAt() method in Java with Example
- Vector ensureCapacity() method in Java with Example
- Vector clear() Method in Java
- Vector addAll() Method in Java
- Vector set() Method in Java
- Vector isEmpty() Method in Java
- Vector lastIndexOf() Method in Java
- Vector addElement() Method in Java
- Vector subList() Method in Java
- Vector add() Method in Java
- Vector capacity() Method in Java
- Vector contains() Method in Java
- Vector containsAll() 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.

