Java Utility Library

Java Vector - removeAllElements() Method



The java.util.Vector.removeAllElements() method is used to remove all elements of the vector. This method makes the vector empty with a size of zero.

Syntax

public void removeAllElements()

Parameters

No parameter is required.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Vector.removeAllElements() method is used to remove all elements of the vector called MyVector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.addElement(10);
    MyVector.addElement(20);
    MyVector.addElement(30);

    //printing vector
    System.out.println("Before applying removeAllElements() method.");
    System.out.println("MyVector contains: " + MyVector);

    MyVector.removeAllElements();

    //printing vector
    System.out.println("\nAfter applying removeAllElements() method."); 
    System.out.println("MyVector contains: " + MyVector);   
  }
}

The output of the above code will be:

Before applying removeAllElements() method.
MyVector contains: [10, 20, 30]

After applying removeAllElements() method.
MyVector contains: []

❮ Java.util - Vector