Java Vector - clear() Method
The java.util.Vector.clear() method is used to clear all elements of the vector. This method makes the vector empty with a size of zero.
Syntax
public void clear()
Parameters
No parameter is required.
Return Value
void type.
Exception
NA
Example:
In the example below, the java.util.Vector.clear() method is used to clear 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 clear() method."); System.out.println("MyVector contains: " + MyVector); MyVector.clear(); //printing vector System.out.println("\nAfter applying clear() method."); System.out.println("MyVector contains: " + MyVector); } }
The output of the above code will be:
Before applying clear() method. MyVector contains: [10, 20, 30] After applying clear() method. MyVector contains: []
❮ Java.util - Vector