Java Vector removeAllElements() Example

The clear() method of java.util.Vector class will remove all the content of the Vector, same-way removeAllElements() method will remove all the elements of the vector.

Signature

public void removeAllElements()

This method removes all of the elements from the vector and sets the size to zero.

Example

The following example shows the usage of java.util.Vector.removeAllElements() method.

import java.util.Vector;

public class RemoveAllElementsMethodVectorExample 
{	
	public static void main(String args[])
	{
		// Create vector1 object
		Vector vector1 = new Vector();
						
		//adding elements to Vector using add()
		vector1.add("Element1");
		vector1.add("Element2");
		vector1.add("Element3");
		vector1.add("Element4");
		
		//Printing the elements of the Vector1
        System.out.println("**Elements of the Vector1 before remove**");
        System.out.println("Size of the vector : "+vector1.size());
        for(String val : vector1)
        {
                System.out.println(val);
        }
        
        vector1.removeAllElements();
        
        //Printing the elements of the Vector1
        System.out.println("**Elements of the Vector1 after remove**");
        System.out.println("Size of the vector : "+vector1.size());
        for(String val : vector1)
        {
                System.out.println(val);
        }
	}
}

Output

**Elements of the Vector1 before remove**
Size of the vector : 4
Element1
Element2
Element3
Element4
**Elements of the Vector1 after remove**
Size of the vector : 0

Leave a Reply

Your email address will not be published. Required fields are marked *