The Java code demonstrates how to use a foreach loop to iterate through the elements of a Queue. The Queue is implemented using a LinkedList, and it contains a list of colors. The program prints all the elements in the queue using the foreach loop. Let's go through the code step-by-step:
import java.util.LinkedList; import java.util.Queue; public class QueueForeachLoop { public static void main(String[] args) { Queue <String> queue_list = new LinkedList <>(); queue_list.add("Orange"); queue_list.add("Yellow"); queue_list.add("Green"); queue_list.add("Pink"); queue_list.add("Black"); queue_list.add("Blue"); queue_list.add("White"); queue_list.add("Red"); System.out.print("Queue Elements : "); // Print queue elements using foreach loop for (String item: queue_list) { System.out.print(item + " "); } } }
Queue Elements : Orange Yellow Green Pink Black Blue White Red
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions