手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#内置队列类Queue用法实例
C#内置队列类Queue用法实例
摘要:本文实例讲述了C#内置队列类Queue用法。分享给大家供大家参考。具体分析如下:这里详细演示了C#内置的队列如何进行添加,移除等功能。usi...

本文实例讲述了C#内置队列类Queue用法。分享给大家供大家参考。具体分析如下:

这里详细演示了C#内置的队列如何进行添加,移除等功能。

using System; using System.Collections.Generic; class Example { public static void Main() { Queue<string> numbers = new Queue<string>(); numbers.Enqueue("one"); numbers.Enqueue("two"); numbers.Enqueue("three"); numbers.Enqueue("four"); numbers.Enqueue("five"); // A queue can be enumerated without disturbing its contents. foreach( string number in numbers ) { Console.WriteLine(number); } Console.WriteLine("nDequeuing '{0}'", numbers.Dequeue()); Console.WriteLine("Peek at next item to dequeue: {0}", numbers.Peek()); Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue()); // Create a copy of the queue, using the ToArray method and the // constructor that accepts an IEnumerable<T>. Queue<string> queueCopy = new Queue<string>(numbers.ToArray()); Console.WriteLine("nContents of the first copy:"); foreach( string number in queueCopy ) { Console.WriteLine(number); } // Create an array twice the size of the queue and copy the // elements of the queue, starting at the middle of the // array. string[] array2 = new string[numbers.Count * 2]; numbers.CopyTo(array2, numbers.Count); // Create a second queue, using the constructor that accepts an // IEnumerable(Of T). Queue<string> queueCopy2 = new Queue<string>(array2); Console.WriteLine("nContents of the second copy, with duplicates and nulls:"); foreach( string number in queueCopy2 ) { Console.WriteLine(number); } Console.WriteLine("nqueueCopy.Contains("four") = {0}", queueCopy.Contains("four")); Console.WriteLine("nqueueCopy.Clear()"); queueCopy.Clear(); Console.WriteLine("nqueueCopy.Count = {0}", queueCopy.Count); } } /* This code example produces the following output: one two three four five Dequeuing 'one' Peek at next item to dequeue: two Dequeuing 'two' Contents of the copy: three four five Contents of the second copy, with duplicates and nulls: three four five queueCopy.Contains("four") = True queueCopy.Clear() queueCopy.Count = 0 */

希望本文所述对大家的C#程序设计有所帮助。

【C#内置队列类Queue用法实例】相关文章:

C# dynamic关键字的使用方法

C# 调用Delphi dll 实例代码

C#版ftp方法实现类的代码

c#剪切板操作的简单实例

c# 获得局域网主机列表实例

C#的XML两种代码注释实例说明

C#位移的介绍与例子

C#仿密保卡功能的简单实现代码

c#初学简单程序实例代码介绍

C#线程池用法详细介绍

精品推荐
分类导航