Write a Java program to Accessing elements in a Vector


The code you provided is written in Java and demonstrates how to use a Vector collection to store and retrieve elements. In the code, a Vector named vec_list is created and populated with string values.

The add() method is used to add elements to the vec_list vector. Finally, the get() method is called on the vec_list object with an index of 2, which corresponds to the third element in the vector. The element at index 2 is then printed using System.out.println().

Source Code

import java.util.Vector;
public class VectorExample
{
    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(vec_list.get(2));
    }
}

Output

Blue

Example Programs