Write a Java program to remove an item from Vector collection at the specified index


The code you provided is written in Java and demonstrates how to remove an item at a specific index 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_IndexEle: This line declares a public class named Remove_IndexEle.
  • 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(3);: This line removes the item at index 3 from the vector vec_list using the remove() method. The element at index 3, which is "Cherry", is removed from the vector.
  • 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_IndexEle
{
	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(3);
 
		System.out.println("Updated Vector Elements : "+vec_list);
	}
}

Output

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

Example Programs