返回到:VB.Net – 集合
Queue表示对象的先进先出集合。 当您需要项目的先进先出访问时使用。 当您在列表中添加项目时,它被称为enqueue,当您删除项目时,称为deque。
队列类的属性和方法
下表列出了Queue类的一些常用属性:
| 属性 | 描述 |
|---|---|
| Count | Gets the number of elements contained in the Queue. 获取队列中包含的元素数。 |
下表列出了Queue类的一些常用方法:
| S.N | 方法名称和用途 |
|---|---|
| 1 | Public Overridable Sub ClearRemoves all elements from the Queue.从队列中删除所有元素。 |
| 2 | Public Overridable Function Contains (obj As Object) As BooleanDetermines whether an element is in the Queue.确定元素是否在队列中。 |
| 3 | Public Overridable Function Dequeue As ObjectRemoves and returns the object at the beginning of the Queue.删除并返回队列开头的对象。 |
| 4 | Public Overridable Sub Enqueue (obj As Object)Adds an object to the end of the Queue.将对象添加到队列的末尾。 |
| 5 | Public Overridable Function ToArray As Object()Copies the Queue to a new array.将队列复制到新数组。 |
| 6 | Public 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
支付宝
微信