Queue
類代表了先入先出的物件集合。它用於需要先進先出存取專案的情況。當在列表中新增一個專案時稱為入隊,當刪除一個專案時稱為出隊。
下表列出了Queue
類的一些常用屬性:
編號 | 屬性 | 描述 |
---|---|---|
1 | Count |
獲取佇列中包含的元素的數量。 |
下表列出了Queue
類的一些常用方法:
編號 | 方法 | 描述 |
---|---|---|
1 | Public Overridable Sub Clear |
刪除堆疊中的所有元素。 |
2 | Public Overridable Function Contains (obj As Object) As Boolean |
確定元素是否在佇列中。 |
3 | Public Overridable Function Dequeue As Object |
刪除並返回佇列開始處的物件。 |
4 | Public Overridable Sub Enqueue (obj As Object) |
將一個物件新增到佇列的末尾。 |
5 | Public Overridable Function ToArray As Object() |
將佇列複製到新陣列。 |
6 | Public Overridable Sub TrimToSize |
將容量設定為佇列中實際元素的數量。 |
以下範例演示如何使用佇列:
Imports System.Collections
Module MyQueue
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
執行上面範例程式碼,得到以下結果 -
F:\worksp\vb.net\collection>vbc MyQueue.vb
Microsoft (R) Visual Basic Compiler version 14.0.1038
for Visual Basic 2012
Copyright (c) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to Visual Basic 2012, which is no longer the latest version. For compilers that support newer versions of the Visual Basic programming language, see http://go.microsoft.com/fwlink/?LinkID=533241
F:\worksp\vb.net\collection>MyQueue.exe
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