Queue 類別

定義

表示物件的先進先出 (FIFO) 集合。

public ref class Queue : System::Collections::ICollection
public ref class Queue : ICloneable, System::Collections::ICollection
public class Queue : System.Collections.ICollection
public class Queue : ICloneable, System.Collections.ICollection
[System.Serializable]
public class Queue : ICloneable, System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Queue : ICloneable, System.Collections.ICollection
type Queue = class
    interface ICollection
    interface IEnumerable
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Queue = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Queue = class
    interface ICollection
    interface ICloneable
    interface IEnumerable
Public Class Queue
Implements ICollection
Public Class Queue
Implements ICloneable, ICollection
繼承
Queue
屬性
實作

範例

下列範例示範如何建立和新增值至 , Queue 以及如何列印其值。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "Hello" );
   myQ->Enqueue( "World" );
   myQ->Enqueue( "!" );
   
   // Displays the properties and values of the Queue.
   Console::WriteLine( "myQ" );
   Console::WriteLine( "\tCount:    {0}", myQ->Count );
   Console::Write( "\tValues:" );
   PrintValues( myQ );
}

void PrintValues( IEnumerable^ myCollection )
{
   IEnumerator^ myEnum = myCollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 myQ
     Count:    3
     Values:    Hello    World    !
*/
 using System;
 using System.Collections;
 public class SamplesQueue  {

    public static void Main()  {

       // Creates and initializes a new Queue.
       Queue myQ = new Queue();
       myQ.Enqueue("Hello");
       myQ.Enqueue("World");
       myQ.Enqueue("!");

       // Displays the properties and values of the Queue.
       Console.WriteLine( "myQ" );
       Console.WriteLine( "\tCount:    {0}", myQ.Count );
       Console.Write( "\tValues:" );
       PrintValues( myQ );
    }

    public static void PrintValues( IEnumerable myCollection )  {
       foreach ( Object obj in myCollection )
          Console.Write( "    {0}", obj );
       Console.WriteLine();
    }
 }
 /*
 This code produces the following output.

 myQ
     Count:    3
     Values:    Hello    World    !
*/
Imports System.Collections

Public Class SamplesQueue

    Public Shared Sub Main()

        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("Hello")
        myQ.Enqueue("World")
        myQ.Enqueue("!")

        ' Displays the properties and values of the Queue.
        Console.WriteLine("myQ")
        Console.WriteLine("    Count:    {0}", myQ.Count)
        Console.Write("    Values:")
        PrintValues(myQ)

    End Sub

    Public Shared Sub PrintValues(myCollection As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myCollection
            Console.Write("    {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' myQ
'     Count:    3
'     Values:    Hello    World    !

備註

這個類別會將佇列實作為迴圈陣列。 儲存在 中的 Queue 物件會插入一端,並從另一端移除。

重要

不建議您將 類別用於 Queue 新的開發。 相反地,建議您使用泛型 Queue<T> 類別。 如需詳細資訊,請參閱不應在 GitHub 上使用 非泛型集合

當您需要暫時儲存以取得資訊時,佇列和堆疊很有用;也就是說,當您想要在擷取專案值之後捨棄專案時。 如果您需要以儲存在集合中的相同順序存取訊號,請使用 Queue 。 如果您需要以反向順序存取訊號,請使用 Stack 。 如果您需要同時從多個執行緒存取集合,請使用 ConcurrentQueue<T>ConcurrentStack<T>

您可以在 和 其元素上 Queue 執行三個主要作業:

Queue 容量是 可以保留的專案 Queue 數目。 當元素新增至 Queue 時,容量會自動透過重新配置而增加。 呼叫 即可減少 TrimToSize 容量。

成長因數是當需要更大的容量時,目前的容量乘以的數位。 當 建構 時 Queue ,會決定成長因數。 預設成長因數為 2.0。 的容量 Queue 一律會至少增加四個,不論成長因數為何。 例如,當需要更大的容量時, Queue 成長因數為 1.0 的 一律會增加四個容量。

Queue 接受 null 為有效值,並允許重複的專案。

如需此集合的泛型版本,請參閱 System.Collections.Generic.Queue<T>

建構函式

Queue()

初始化 Queue 類別的新執行個體,其為空白、具有預設初始容量且使用預設的等比級數因數。

Queue(ICollection)

初始化 Queue 類別的新執行個體,其中含有從指定之集合複製過來的項目、具有與複製的項目數一樣的初始容量且使用預設的等比級數因數。

Queue(Int32)

初始化 Queue 類別的新執行個體,其為空白、具有初始容量且使用預設的等比級數因數。

Queue(Int32, Single)

初始化 Queue 類別的新執行個體,其為空白、具有初始容量且使用指定的等比級數因數。

屬性

Count

取得 Queue 中所包含的項目數。

IsSynchronized

取得值,這個值表示對 Queue 的存取是否同步 (安全執行緒)。

SyncRoot

取得可用以同步存取 Queue 的物件。

方法

Clear()

Queue 移除所有物件。

Clone()

建立 Queue 的淺層複本。

Contains(Object)

判斷某項目是否在 Queue 中。

CopyTo(Array, Int32)

從指定的陣列索引處開始,複製 Queue 項目至現有一維 Array

Dequeue()

移除並傳回在 Queue 開頭的物件。

Enqueue(Object)

將物件加入至 Queue 的末端。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回在 Queue 中逐一查看的列舉值。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Peek()

傳回 Queue 開頭的物件而不移除它。

Synchronized(Queue)

傳回包裝原始佇列並且為安全執行緒的新 Queue

ToArray()

Queue 項目複製到新的陣列。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TrimToSize()

將容量設為 Queue 中的實際項目數目。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

執行緒安全性

Visual Basic 中的公用靜態 (Shared) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。

若要保證 的 Queue 執行緒安全性,所有作業都必須透過 方法傳 Synchronized(Queue) 回的包裝函式來完成。

透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱