Write a Java program to remove all elements of Vector collection using removeAllElements() method


The code you provided demonstrates how to remove all elements from a Vector using the removeAllElements method in Java. Here's a breakdown of what the code does:

  • A Vector<String> object named vec_list is created.
  • Elements "Red", "Green", "Blue", "Yellow", and "Orange" are added to vec_list using the add method.
  • The initial contents of vec_list are printed using System.out.println.
  • The removeAllElements method is called on vec_list, which removes all elements from the vector.
  • The updated contents of vec_list are printed using System.out.println.

Source Code

import java.util.*;
public class RemoveAllElements
{
	public static void main(String[] args)
	{
		Vector < String > vec_list = new Vector < String > ();
		vec_list.add("Red");
		vec_list.add("Green");
		vec_list.add("Blue");
		vec_list.add("Yellow");
		vec_list.add("Orange");
 
		System.out.println("Vector Elements : " + vec_list);
 
		vec_list.removeAllElements();
		System.out.println("Vector Elements : " + vec_list);
	}
}

Output

Vector Elements : [Red, Green, Blue, Yellow, Orange]
Vector Elements : []

Example Programs