Write a Java program to remove a specified item from Vector collection


The code you provided is written in Java and demonstrates how to remove an item from a Vector using the remove() method. Here's an explanation of the code:

  • import java.util.*;: This line imports the java.util package, which contains the Vector class and other utility classes.
  • public class Remove_Item: This line declares a public class named Remove_Item.
  • public static void main(String[] args): This is the main method where the execution of the program starts. It takes an array of strings as command line arguments.
  • Vector<String> vec_list = new Vector<String>();: This line declares and initializes a Vector object named vec_list that can store String values. Note that using generics (<String>) is not necessary in newer versions of Java.
  • vec_list.add("Apple");: This line adds the string "Apple" to the vector vec_list using the add() method.
  • vec_list.add("Banana");: This line adds the string "Banana" to the vector vec_list using the add() method.
  • vec_list.add("Orange");: This line adds the string "Orange" to the vector vec_list using the add() method.
  • vec_list.add("Cherry");: This line adds the string "Cherry" to the vector vec_list using the add() method.
  • vec_list.add("Mango");: This line adds the string "Mango" to the vector vec_list using the add() method.
  • System.out.println("Vector Elements : "+vec_list);: This line prints the elements of the vector by concatenating the string "Vector Elements :" with the vector vec_list. The println function automatically converts the vector to a string representation.
  • vec_list.remove("Orange");: This line removes the item "Orange" from the vector vec_list using the remove() method. If the item is found in the vector, it will be removed.
  • System.out.println("Updated Vector Elements : "+vec_list);: This line prints the elements of the vector after the removal using the remove() method.

Source Code

import java.util.*;
public class Remove_Item
{
	public static void main(String[] args)
	{
		Vector <String> vec_list = new Vector <String>();
 
		vec_list.add("Apple");
		vec_list.add("Banana");
		vec_list.add("Orange");
		vec_list.add("Cherry");
		vec_list.add("Mango");
 
		System.out.println("Vector Elements : "+vec_list);
		vec_list.remove("Orange");
 
		System.out.println("Updated Vector Elements : "+vec_list);
	}
}

Output

Vector Elements : [Apple, Banana, Orange, Cherry, Mango]
Updated Vector Elements : [Apple, Banana, Cherry, Mango]

Example Programs