返回顶部

收藏

C# 内置队列类Queue使用范例

更多
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
 */

标签:queue,队列,C#

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. mdjhny 发表 2013-03-05 16:00:00 CSDN文章多线程下载器
  2. 博主 发表 2012-04-04 00:00:00 uWSGI listen queue 队列溢出的问题
  3. 博主 发表 2013-01-31 10:54:15 远程调用服务(RPC)和消息(Message Queue)对比及其适用/不适用场合
  4. Herb Sutter 发表 2012-08-02 11:26:37 “Strong” and “weak” hardware memory models
  5. Conda 发表 2013-04-11 03:18:11 PBS 队列配置
  6. admin 发表 2013-06-24 02:46:00 自动化工具word文档批量转html
  7. admin 发表 2013-08-25 17:48:41 asp.net验证码操作总结
  8. 97world 发表 2013-11-16 12:55:37 .Net实践:C#如何读取、写入INI配置文件
  9. 嘟嘟 发表 2013-09-08 04:59:01 c#坐标变换
  10. 博主 发表 2009-08-25 04:00:00 c#代码测试。。。也是本人人生第一个c#程序
  11. 张浩浩 发表 2014-07-19 14:22:50 使用C#开发ActiveX控件全攻略
  12. jonskeet 发表 2014-08-08 14:48:04 The BobbyTables culture

发表评论