程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#內置隊列類Queue用法實例

C#內置隊列類Queue用法實例

編輯:C#入門知識

C#內置隊列類Queue用法實例。本站提示廣大學習愛好者:(C#內置隊列類Queue用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#內置隊列類Queue用法實例正文


本文實例講述了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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved