Write a Java program to Iterating over a Vector using an enhanced for loop


The code you provided is written in Java and demonstrates how to use an enhanced for loop (also known as a for-each loop) to iterate over the elements of a Vector collection and print them. In the code, a Vector named vec_list is created and populated with string values.

The enhanced for loop is used to iterate over each element in the vec_list vector. The loop variable col is of type String, which represents the type of elements in the vector. For each iteration, the current element is assigned to col, and then it is printed using System.out.println().

Source Code

import java.util.Vector;
public class Enhance_Loop
{
    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");
 
        for (String col : vec_list)
		{
            System.out.println(col);
        }
    }
}

Output

Red
Green
Blue
Yellow
Orange

Example Programs