数组集合篇-Queue类的使用
Queue(队列):遵循的是FIFO机制,先入先出机制。元素在队列的尾部插入(入队),并且从顶部移除(出队)。
using System;
using System.Collections;
.........
Queue numbers = new Queue();
//填充队列
foreach(int number in new int[4]{9,3,7,2})
{
numbers.Enqueue(number);
Console.WriteLine(number + "has joined the queue");
}
.........
//遍历队列
foreach(int number in numbers)
{
Console.WriteLine(number);
}
//清空队列
while (numbers.Count !=0)
{
int number = (int)numbers.Dequeue();//注意这里又有拆箱操作,入队的时候装箱是自动的。
Console.WriteLine(number + " has left the queue");
}
上述代码输出如下:
9 has joined the queue
3 has joined the queue
7 has joined the queue
2 has joined the queue
9
3
7
2
9 has left the queue
3 has left the queue
7 has left the queue
2 has left the queue
浙公网安备 33010602011771号