Skip to content
Related Articles

Related Articles

Improve Article

Vector removeAllElements() method in Java with Example

  • Last Updated : 24 Dec, 2018

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);
    }
}
Output:
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);
    }
}
Output:
Vector: [10, 15, 30, 20, 5]
The final Vector: []

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :