19.4. Queue

As the name implies, a Queue encapsulates a standard FIFO abstract data type. Queues are useful for storing objects to be processed in the order that they are received. The three most commonly used methods of a Queue are Peek, Enqueue, and Dequeue. Like an ArrayList, the capacity of a Queue object changes automatically as more objects are inserted.

The program below demonstrates how Queue can be used. The output is interspersed with the code to show the outputs of the different sections.

 1: using System;
 2: using System.Collections;
 3:
 4: public class TestClass{
 5:   public static void Main(){
 6:
 7:     Queue q = new Queue();
 8:
 9:     // Using Enqueue 10: q.Enqueue("A"); 11: q.Enqueue("B"); 12: q.Enqueue("C"); 13: q.Enqueue("D"); 14: q.Enqueue("E"); ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.