Write a Java program to get the last element of Vector using the lastElement() method


The code you provided demonstrates how to retrieve the last element of a Vector using the lastElement method in Java. Here's a breakdown of what the code does:

  • A Vector<String> object named vec_list is created.
  • Elements "Papaya", "Apple", "Grapes", "Cherry", "Orange", and "Banana" are added to vec_list using the add method.
  • The lastElement method is used to retrieve the last element of vec_list, which is the element "Banana". The last element is printed using System.out.println.

Source Code

import java.util.*;
public class GetLast_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("Cherry");
		vec_list.add("Orange");
		vec_list.add("Banana");
 
		System.out.println("Last Element: " + vec_list.lastElement());
	}
}

Output

Last Element: Banana

Example Programs