VB.Net 教程:Queue 队列

返回到:VB.Net – 集合

Queue表示对象的先进先出集合。 当您需要项目的先进先出访问时使用。 当您在列表中添加项目时,它被称为enqueue,当您删除项目时,称为deque。

队列类的属性和方法

下表列出了Queue类的一些常用属性:

属性描述
CountGets the number of elements contained in the Queue.
获取队列中包含的元素数。

下表列出了Queue类的一些常用方法:

S.N方法名称和用途
1Public Overridable Sub ClearRemoves all elements from the Queue.从队列中删除所有元素。
2Public Overridable Function Contains (obj As Object) As BooleanDetermines whether an element is in the Queue.确定元素是否在队列中。
3Public Overridable Function Dequeue As ObjectRemoves and returns the object at the beginning of the Queue.删除并返回队列开头的对象。
4Public Overridable Sub Enqueue (obj As Object)Adds an object to the end of the Queue.将对象添加到队列的末尾。
5Public Overridable Function ToArray As Object()Copies the Queue to a new array.将队列复制到新数组。
6Public Overridable Sub TrimToSizeSets the capacity to the actual number of elements in the Queue.将容量设置为队列中实际的元素数。

示例:

以下示例演示如何使用队列:

Module collections
   Sub Main()
      Dim q As Queue = New Queue()
      q.Enqueue("A")
      q.Enqueue("M")
      q.Enqueue("G")
      q.Enqueue("W")
      Console.WriteLine("Current queue: ")
      Dim c As Char
      For Each c In q
          Console.Write(c + " ")
      Next c
      Console.WriteLine()
      q.Enqueue("V")
      q.Enqueue("H")
      Console.WriteLine("Current queue: ")
      For Each c In q
          Console.Write(c + " ")
      Next c
      Console.WriteLine()
      Console.WriteLine("Removing some values ")
      Dim ch As Char
      ch = q.Dequeue()
      Console.WriteLine("The removed value: {0}", ch)
      ch = q.Dequeue()
      Console.WriteLine("The removed value: {0}", ch)
      Console.ReadKey()
   End Sub
End Module

当上述代码被编译和执行时,它产生以下结果:

Current queue: 
A M G W 
Current queue: 
A M G W V H 
Removing some values
The removed value: A
The removed value: M

返回到:VB.Net – 集合

作者:terry,如若转载,请注明出处:https://www.web176.com/vbnet_api/11489.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年3月1日
下一篇 2023年3月1日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注