Open In App
Related Articles

Vector removeAllElements() method in Java with Example

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

// 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

// 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: []

Time complexity: O(n). // n is the size of the vector.
Auxiliary space: O(1)



Last Updated : 24 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads