# Clear out all of the Vector elements in Java

Java 8Object Oriented ProgrammingProgramming

A Vector can be cleared in Java using the method java.util.Vector.clear(). This method removes all the elements in the Vector. There are no parameters required by the Vector.clear() method and it returns no value.

A program that demonstrates this is given as follows −

## Example

Live Demo

import java.util.Vector;
public class Demo {
public static void main(String args[]) {
Vector vec = new Vector(5);
System.out.println("The Vector elements are: " + vec);
vec.clear();
System.out.println("The Vector elements are: " + vec);
}
}

The output of the above program is as follows −

The Vector elements are: [4, 1, 3, 9, 6, 2, 8]
The Vector elements are: []

Now let us understand the above program.

The Vector is created. Then Vector.add() is used to add the elements to the Vector. The Vector is displayed before and after using the Vector.clear() method which clears the Vector. A code snippet which demonstrates this is as follows −

Vector vec = new Vector(5);
System.out.println("The Vector elements are: " + vec);