A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/vector-removeallelements-method-in-java-with-example/ below:

Vector removeAllElements() method in Java with Example

Vector removeAllElements() method in Java with Example

Last Updated : 11 Jul, 2025

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)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4