[C#] C# 内置队列类Queue使用范例 →→→→→进入此内容的聊天室

来自 , 2019-03-17, 写在 C#, 查看 137 次.
URL http://www.code666.cn/view/26310c70
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Example
  5. {
  6.     public static void Main()
  7.     {
  8.         Queue<string> numbers = new Queue<string>();
  9.         numbers.Enqueue("one");
  10.         numbers.Enqueue("two");
  11.         numbers.Enqueue("three");
  12.         numbers.Enqueue("four");
  13.         numbers.Enqueue("five");
  14.  
  15.         // A queue can be enumerated without disturbing its contents.
  16.         foreach( string number in numbers )
  17.         {
  18.             Console.WriteLine(number);
  19.         }
  20.  
  21.         Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
  22.         Console.WriteLine("Peek at next item to dequeue: {0}",
  23.             numbers.Peek());
  24.         Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
  25.  
  26.         // Create a copy of the queue, using the ToArray method and the
  27.         // constructor that accepts an IEnumerable<T>.
  28.         Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
  29.  
  30.         Console.WriteLine("\nContents of the first copy:");
  31.         foreach( string number in queueCopy )
  32.         {
  33.             Console.WriteLine(number);
  34.         }
  35.  
  36.         // Create an array twice the size of the queue and copy the
  37.         // elements of the queue, starting at the middle of the
  38.         // array.
  39.         string[] array2 = new string[numbers.Count * 2];
  40.         numbers.CopyTo(array2, numbers.Count);
  41.  
  42.         // Create a second queue, using the constructor that accepts an
  43.         // IEnumerable(Of T).
  44.         Queue<string> queueCopy2 = new Queue<string>(array2);
  45.  
  46.         Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
  47.         foreach( string number in queueCopy2 )
  48.         {
  49.             Console.WriteLine(number);
  50.         }
  51.  
  52.         Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
  53.             queueCopy.Contains("four"));
  54.  
  55.         Console.WriteLine("\nqueueCopy.Clear()");
  56.         queueCopy.Clear();
  57.         Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
  58.     }
  59. }
  60.  
  61. /* This code example produces the following output:
  62.  
  63. one
  64. two
  65. three
  66. four
  67. five
  68.  
  69. Dequeuing 'one'
  70. Peek at next item to dequeue: two
  71. Dequeuing 'two'
  72.  
  73. Contents of the copy:
  74. three
  75. four
  76. five
  77.  
  78. Contents of the second copy, with duplicates and nulls:
  79.  
  80.  
  81.  
  82. three
  83. four
  84. five
  85.  
  86. queueCopy.Contains("four") = True
  87.  
  88. queueCopy.Clear()
  89.  
  90. queueCopy.Count = 0
  91.  */
  92.  
  93. //csharp/6153

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

这儿你可以回复上面这条便签

captcha