当前位置: 移动技术网 > IT编程>开发语言>c# > C#内置队列类Queue用法实例

C#内置队列类Queue用法实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文实例讲述了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#程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网