Java Program to Print Queue Elements Using ‘Foreach’ Loop

You are currently viewing Java Program to Print Queue Elements Using ‘Foreach’ Loop

Java program to print Queue elements using ‘foreach’ loop

// Java program to print queue elements 
// using foreach loop

import java.util.LinkedList;
import java.util.Queue;

public class Main {
  public static void main(String[] args) {
    Queue < Integer > queue = new LinkedList < > ();

    queue.add(10);
    queue.add(20);
    queue.add(30);
    queue.add(40);
    queue.add(50);

    System.out.println("Queue elements: ");
    for (int item: queue) {
      System.out.print(item + " ");
    }

  }
}

Output

Queue elements: 
10 20 30 40 50 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply