c#queue
C#Queue.Peek()方法 (C# Queue.Peek() method)
Queue.Peek() method is used to get the object at the beginning of the Queue without removing it.
Queue.Peek()方法用于在Queue的开头获取对象而不删除它。
Syntax:
句法:
Object Queue.Peek();
Parameters: None
参数:无
Return value: Object – it returns an object/element from the beginning of the Queue.
返回值: Object –从队列开始处返回一个对象/元素。
Example:
例:
declare and initialize a Queue:
Queue que = new Queue();
insertting elements:
que.Enqueue(100);
que.Enqueue(200);
que.Enqueue(300);
que.Enqueue(400);
que.Enqueue(500);
printing element from the beginning of Queue:
que.Peek();
Output:
100
C#示例,使用Queue.Peek()方法从队列的开头获取对象/元素 (C# example to get an object/element from the beginning of the Queue using Queue.Peek() method)
using System;
using System.Text;
using System.Collections;
namespace Test
{
class Program
{
//function to print queue elements
static void printQueue(Queue q)
{
foreach (Object obj in q)
{
Console.Write(obj + " ");
}
Console.WriteLine();
}
static void Main(string[] args)
{
//declare and initialize a Queue
Queue que = new Queue();
//insertting elements
que.Enqueue(100);
que.Enqueue(200);
que.Enqueue(300);
que.Enqueue(400);
que.Enqueue(500);
//printing queue elements
Console.WriteLine("Queue elements are...");
printQueue(que);
//printing element from the beginning of Queue
Object item = que.Peek();
Console.WriteLine("Queue's beginning element: " + item);
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output
输出量
Queue elements are...
100 200 300 400 500
Queue's beginning element: 100
Reference: Queue.Peek Method
参考: Queue.Peek方法
翻译自: https://www.includehelp.com/dot-net/queue-peek-method-with-example-in-c-sharp.aspx
c#queue