0 0

## C#集合系列~Queue用法

2013-12-11
 Queue与Stack类似，主要区别是Queue类以先进先出(FIFO)的结构创建集合，即，元素进入集合的顺序与弹出顺序相同一、void Enqueue(object obj):将元素加入队列            Queue qe = new Queue();            qe.Enqueue(1);            qe.Enqueue(2);            qe.Enqueue(3);                        foreach(int i in qe)            {                Console.Write(i+" ");            }输出：1　2　3二、object Dequeue():将队首元素从队列中删除            Queue qe = new Queue();            qe.Enqueue(1);            qe.Enqueue(2);            qe.Enqueue(3);            int item = (int)qe.Dequeue();//item=1                        foreach(int i in qe)            {                Console.Write(i+" ");            }输出：2　3三、bool Contains(object obj):判断是否包含某元素      qe.Contains(3); //返回true　 qe.Contains(5); //返回false四、public virtual object Clone():创建集合的浅度拷贝            Queue qe = new Queue();            qe.Enqueue("a");            qe.Enqueue("b");            qe.Enqueue("c");                        Queue qq = (Queue)qe.Clone();                        foreach (string item in qq)            {                Console.Write(item + " ");            }输出：a b c示例：using System;using System.Collections.Generic;using System.Text;using System.Collections;namespace ConsoleApplication1{　 class Program   {   static void Main(string[] args){     Queue qu = new Queue();     Queue qu2 = new Queue();     foreach (int i in new int[4] ...{ 1, 2, 3, 4 })   {       qu.Enqueue(i);//填充       qu2.Enqueue(i);    }    foreach (int i in qu)　{Console.WriteLine(i);//遍历}qu.Dequeue();Console.WriteLine("Dequeue");foreach (int i in qu){Console.WriteLine(i);}qu2.Peek();//弹出最后一项不删除Console.WriteLine("Peek");foreach (int i in qu2)｛Console.WriteLine(i);}while (qu2.Count != 0){int i = (int)qu2.Dequeue();//清空qu2.Dequeue();//清空}Console.WriteLine("清空");foreach (int i in qu2){Console.WriteLine(i);}}}}

0条评论