Write a Java program to get the first element of Vector using the firstElement() method


The code you provided is written in Java and demonstrates how to get the first element from a Vector. Here's a breakdown of what the code does:

  • A Vector object vec_list is created using the Vector class. It is declared to store elements of type String.
  • Several elements are added to vec_list using the add() method. The elements are strings representing fruits: "Papaya", "Apple", "Grapes", "Banana", "Cherry", and "Orange".
  • The first element of the vector is retrieved using the firstElement() method.
  • The first element is printed using System.out.println().

Source Code

import java.util.*;
public class GetFirst_Elements
{
	public static void main(String[] args)
	{
		Vector < String > vec_list = new Vector < String > ();
		vec_list.add("Papaya");
		vec_list.add("Apple");
		vec_list.add("Grapes");
		vec_list.add("Banana");
		vec_list.add("Cherry");
		vec_list.add("Orange");
 
		System.out.println("First Element : " + vec_list.firstElement());
	}
}

Output

First Element : Papaya

Example Programs