Write a Java program to get element from Vector collection at the specified index


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

  • A Vector<String> object named vec_list is created.
  • An integer variable ind is initialized with a value of 3, representing the index of the element we want to retrieve.
  • Elements "Apple", "Banana", "Cherry", "Orange", and "Mango" are added to vec_list using the add method.
  • The current contents of vec_list are printed using System.out.println.
  • The elementAt method is used to retrieve the element at index ind from vec_list.
  • The element at the specified index is printed using System.out.println, along with the corresponding index value.

Source Code

import java.util.*;
public class Get_Elements
{
	public static void main(String[] args)
	{
		Vector < String > vec_list = new Vector < String > ();
		int ind = 3;
		vec_list.add("Apple");
		vec_list.add("Banana");
		vec_list.add("Cherry");
		vec_list.add("Orange");
		vec_list.add("Mango");
 
		System.out.println("Vector Elements : " + vec_list);
		System.out.println("Element at index " + ind + " is : " + vec_list.elementAt(ind));
	}
}

Output

Vector Elements : [Apple, Banana, Cherry, Orange, Mango]
Element at index 3 is : Orange

Example Programs