Vector removeAllElements() method in Java with Example

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Vector: [Welcome, To, Geeks, 4, Geeks]
The final Vector: []

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Vector: [10, 15, 30, 20, 5]
The final Vector: []


My Personal Notes arrow_drop_up

All powers are within you You can do anything and everything Believe in that

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.