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

如下的代码是关于C# 内置队列类Queue使用范例的代码,应该是对各朋友也有好处。

using System;

using System.Collections.Generic;

class Example

{

    public static void Main()

    {

        Queue numbers = new Queue();

        numbers.Enqueue("one");

        numbers.Enqueue("two");

        numbers.Enqueue("three");

        numbers.Enqueue("four");

        numbers.Enqueue("five");

        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());

        Queue queueCopy = new Queue(numbers.ToArray());

        Console.WriteLine("nContents of the first copy:");

        foreach( string number in queueCopy )

        {

            Console.WriteLine(number);

        }

        numbers.CopyTo(array2, numbers.Count);

        Queue queueCopy2 = new Queue(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);

    }

}

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# 内置队列类Queue使用范例的代码)