VB.Net - Queue隊(duì)列

2018-12-16 17:15 更新
Queue表示對(duì)象的先進(jìn)先出集合。 當(dāng)您需要項(xiàng)目的先進(jìn)先出訪問(wèn)時(shí)使用。 當(dāng)您在列表中添加項(xiàng)目時(shí),它被稱為enqueue,當(dāng)您刪除項(xiàng)目時(shí),稱為deque。


隊(duì)列類的屬性和方法

下表列出了Queue類的一些常用屬性:

屬性描述
CountGets the number of elements contained in the Queue.
獲取隊(duì)列中包含的元素?cái)?shù)。

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

S.N方法名稱和用途
1

Public Overridable Sub Clear

Removes all elements from the Queue.

從隊(duì)列中刪除所有元素。

2

Public Overridable Function Contains (obj As Object) As Boolean

Determines whether an element is in the Queue.

確定元素是否在隊(duì)列中。

3

Public Overridable Function Dequeue As Object

Removes and returns the object at the beginning of the Queue.

刪除并返回隊(duì)列開頭的對(duì)象。

4

Public Overridable Sub Enqueue (obj As Object)

Adds an object to the end of the Queue.

將對(duì)象添加到隊(duì)列的末尾。

5

Public Overridable Function ToArray As Object()

Copies the Queue to a new array.

將隊(duì)列復(fù)制到新數(shù)組。

6

Public Overridable Sub TrimToSize

Sets the capacity to the actual number of elements in the Queue.

將容量設(shè)置為隊(duì)列中實(shí)際的元素?cái)?shù)。


示例:

以下示例演示如何使用隊(duì)列:
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

當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生以下結(jié)果:
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

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)