获取队列开头的对象 - 在 C# 中窥视操作
要获取队列开头的对象,代码如下所示——
示例
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); queue.Enqueue("F"); queue.Enqueue("G"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); } }
输出
这将产生以下输出——
Count of elements = 7 Element at the beginning of queue = A
示例
让我们看另一个示例——
using System; using System.Collections.Generic; public class Demo { public static void Main() { Queue<string> queue = new Queue<string>(); queue.Enqueue("A"); queue.Enqueue("B"); queue.Enqueue("C"); queue.Enqueue("D"); queue.Enqueue("E"); queue.Enqueue("F"); queue.Enqueue("G"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); queue.Enqueue("H"); queue.Enqueue("I"); queue.Enqueue("J"); Console.WriteLine("Count of elements = "+queue.Count); Console.WriteLine("Element at the beginning of queue = " + queue.Peek()); } }
输出
这将产生以下输出——
Count of elements = 7 Element at the beginning of queue = A Count of elements = 10 Element at the beginning of queue = A
广告