Write a Java program to get the enumeration of the values present in the Vector


The code you provided demonstrates how to retrieve and iterate over the elements of a Vector using an Enumeration in Java. Here's a breakdown of what the code does:

  • A Vector<String> object named vec_list is created.
  • Elements "Apple", "Grapes", "Banana", "Mango", "Cherry", and "Orange" are added to vec_list using the add method.
  • The elements method is used on vec_list to obtain an Enumeration object called enum_val. This Enumeration represents a snapshot of the vector's elements at the time of the call.
  • The hasMoreElements method is used to check if there are more elements in the Enumeration.
  • Inside the while loop, the nextElement method is used to retrieve and print each element of the Enumeration.
  • The loop continues until there are no more elements in the Enumeration.

Source Code

import java.util.*;
public class Get_EnumValues
{
	public static void main(String[] args)
	{
		Vector < String > vec_list = new Vector < String > ();
		vec_list.add("Apple");
		vec_list.add("Grapes");
		vec_list.add("Banana");
		vec_list.add("Mango");
		vec_list.add("Cherry");
		vec_list.add("Orange");
 
		Enumeration enum_val = vec_list.elements();
 
		System.out.println("The Enumeration of Values are : ");
		while (enum_val.hasMoreElements())
		{
			System.out.println(enum_val.nextElement());
		}
	}
}

Output

The Enumeration of Values are :
Apple
Grapes
Banana
Mango
Cherry
Orange

Example Programs