Queue<T> 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示物件的先進先出 (FIFO) 集合。
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
public class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface IReadOnlyCollection<'T>
interface ICollection
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type Queue<'T> = class
interface seq<'T>
interface ICollection
interface IReadOnlyCollection<'T>
interface IEnumerable
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T), IReadOnlyCollection(Of T)
Public Class Queue(Of T)
Implements ICollection, IEnumerable(Of T)
類型參數
- T
指定佇列中項目的類型。
- 繼承
-
Queue<T>
- 屬性
- 實作
範例
下列程式碼範例示範泛型類別的 Queue<T> 數種方法。 程式碼範例會建立具有預設容量的字串佇列,並使用 Enqueue 方法來排入五個字串。 會列舉佇列的專案,這不會變更佇列的狀態。 方法 Dequeue 可用來取消佇列第一個字串。 方法 Peek 可用來查看佇列中的下一個專案,然後使用 Dequeue 方法將它取消佇列。
方法 ToArray 可用來建立陣列,並將佇列元素複製到該陣列,然後將陣列傳遞至 Queue<T> 採用 IEnumerable<T> 的建構函式,以建立佇列的複本。 複本的專案隨即顯示。
建立佇列大小的兩次陣列,而 CopyTo 方法會用來複製陣列元素,從陣列中間開始。 建 Queue<T> 構函式會再次用來建立佇列的第二個複本,其中包含開頭有三個 Null 元素。
方法 Contains 用來顯示字串 「four」 位於佇列的第一個複本中,之後 Clear 方法會清除複本,而 Count 屬性會顯示佇列是空的。
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");
// A queue can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nDequeuing '{0}'", numbers.Dequeue());
Console.WriteLine("Peek at next item to dequeue: {0}",
numbers.Peek());
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue());
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in queueCopy )
{
Console.WriteLine(number);
}
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
Queue<string> queueCopy2 = new Queue<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in queueCopy2 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nqueueCopy.Contains(\"four\") = {0}",
queueCopy.Contains("four"));
Console.WriteLine("\nqueueCopy.Clear()");
queueCopy.Clear();
Console.WriteLine("\nqueueCopy.Count = {0}", queueCopy.Count);
}
}
/* This code example produces the following output:
one
two
three
four
five
Dequeuing 'one'
Peek at next item to dequeue: two
Dequeuing 'two'
Contents of the first copy:
three
four
five
Contents of the second copy, with duplicates and nulls:
three
four
five
queueCopy.Contains("four") = True
queueCopy.Clear()
queueCopy.Count = 0
*/
open System
open System.Collections.Generic
let numbers = Queue()
numbers.Enqueue "one"
numbers.Enqueue "two"
numbers.Enqueue "three"
numbers.Enqueue "four"
numbers.Enqueue "five"
// A queue can be enumerated without disturbing its contents.
for number in numbers do
printfn $"{number}"
printfn $"\nDequeuing '{numbers.Dequeue()}'"
printfn $"Peek at next item to dequeue: {numbers.Peek()}"
printfn $"Dequeuing '{numbers.Dequeue()}'"
// Create a copy of the queue, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
let queueCopy = numbers.ToArray() |> Queue
printfn $"\nContents of the first copy:"
for number in queueCopy do
printfn $"{number}"
// Create an array twice the size of the queue and copy the
// elements of the queue, starting at the middle of the
// array.
let array2 = numbers.Count * 2 |> Array.zeroCreate
numbers.CopyTo(array2, numbers.Count)
// Create a second queue, using the constructor that accepts an
// IEnumerable(Of T).
let queueCopy2 = Queue array2
printfn $"\nContents of the second copy, with duplicates and nulls:"
for number in queueCopy2 do
printfn $"{number}"
printfn $"""\nqueueCopy.Contains "four" = {queueCopy.Contains "four"}"""
printfn $"\nqueueCopy.Clear()"
queueCopy.Clear()
printfn $"queueCopy.Count = {queueCopy.Count}"
// This code example produces the following output:
// one
// two
// three
// four
// five
//
// Dequeuing 'one'
// Peek at next item to dequeue: two
// Dequeuing 'two'
//
// Contents of the first copy:
// three
// four
// five
//
// Contents of the second copy, with duplicates and nulls:
//
//
//
// three
// four
// five
//
// queueCopy.Contains "four" = True
//
// queueCopy.Clear()
//
// queueCopy.Count = 0
Imports System.Collections.Generic
Module Example
Sub Main
Dim numbers As New Queue(Of String)
numbers.Enqueue("one")
numbers.Enqueue("two")
numbers.Enqueue("three")
numbers.Enqueue("four")
numbers.Enqueue("five")
' A queue can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "Dequeuing '{0}'", numbers.Dequeue())
Console.WriteLine("Peek at next item to dequeue: {0}", _
numbers.Peek())
Console.WriteLine("Dequeuing '{0}'", numbers.Dequeue())
' Create a copy of the queue, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T).
Dim queueCopy As New Queue(Of String)(numbers.ToArray())
Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In queueCopy
Console.WriteLine(number)
Next
' Create an array twice the size of the queue, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the queue, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)
' Create a second queue, using the constructor that accepts an
' IEnumerable(Of T).
Dim queueCopy2 As New Queue(Of String)(array2)
Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In queueCopy2
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "queueCopy.Contains(""four"") = {0}", _
queueCopy.Contains("four"))
Console.WriteLine(vbLf & "queueCopy.Clear()")
queueCopy.Clear()
Console.WriteLine(vbLf & "queueCopy.Count = {0}", _
queueCopy.Count)
End Sub
End Module
' This code example produces the following output:
'
'one
'two
'three
'four
'five
'
'Dequeuing 'one'
'Peek at next item to dequeue: two
'
'Dequeuing 'two'
'
'Contents of the copy:
'three
'four
'five
'
'Contents of the second copy, with duplicates and nulls:
'
'
'
'three
'four
'five
'
'queueCopy.Contains("four") = True
'
'queueCopy.Clear()
'
'queueCopy.Count = 0
備註
這個類別會將泛型佇列實作為迴圈陣列。 儲存在 中的 Queue<T> 物件會插入一端,並從另一端移除。 當您需要暫時儲存以取得資訊時,佇列和堆疊很有用;也就是說,當您想要在擷取專案值之後捨棄專案時。 如果您需要以儲存在集合中的相同順序存取訊號,請使用 Queue<T> 。 如果您需要以反向順序存取訊號,請使用 Stack<T> 。 如果您需要同時從多個執行緒存取集合,請使用 ConcurrentQueue<T> 或 ConcurrentStack<T> 。
您可以在 和 其元素上 Queue<T> 執行三個主要作業:
的 Queue<T> 容量是 可以保留的專案 Queue<T> 數目。 當元素新增至 Queue<T> 時,重新配置內部陣列會自動增加容量。 呼叫 即可減少 TrimExcess 容量。
Queue<T> 接受 null
作為參考型別的有效值,並允許重複的專案。
建構函式
Queue<T>() |
初始化 Queue<T> 類別的新執行個體,這個執行個體為空白且具有預設的初始容量。 |
Queue<T>(IEnumerable<T>) |
初始化 Queue<T> 類別的新執行個體,其包含從指定之集合複製的項目,且具有容納複製之項目數目的足夠容量。 |
Queue<T>(Int32) |
為具有指定初始容量且為空的 Queue<T> 類別,初始化新的執行個體。 |
屬性
Count |
取得 Queue<T> 中所包含的項目數。 |
方法
Clear() |
從 Queue<T> 移除所有物件。 |
Contains(T) |
判斷某項目是否在 Queue<T> 中。 |
CopyTo(T[], Int32) | |
Dequeue() |
移除並傳回在 Queue<T> 開頭的物件。 |
Enqueue(T) |
將物件加入至 Queue<T> 的末端。 |
EnsureCapacity(Int32) |
確保此佇列的容量至少是指定的 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
傳回在 Queue<T> 中逐一查看的列舉值。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
Peek() |
傳回 Queue<T> 開頭的物件而不移除它。 |
ToArray() |
將 Queue<T> 項目複製到新的陣列。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
TrimExcess() |
如果該數目小於目前容量的 90%,則將容量設為 Queue<T> 中項目的實際數目。 |
TryDequeue(T) |
移除 開頭的 Queue<T> 物件,並將它 |
TryPeek(T) |
傳回值,這個值表示 在 開頭 Queue<T> 是否有 物件,如果存在物件,則會將它 |
明確介面實作
ICollection.CopyTo(Array, Int32) |
從特定的 ICollection 索引開始,將 Array 的項目複製到 Array。 |
ICollection.IsSynchronized |
取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。 |
ICollection.SyncRoot |
取得可用以同步存取 ICollection 的物件。 |
IEnumerable.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
IEnumerable<T>.GetEnumerator() |
傳回逐一查看集合的列舉值。 |
擴充方法
適用於
執行緒安全性
Visual Basic 中的公用靜態 (Shared
) 此類型的成員是安全線程。 並非所有的執行個體成員都是安全執行緒。
只要未修改集合,就可以 Queue<T> 同時支援多個讀取器。 即使如此,透過集合列舉本質上不是安全線程程式。 如需安全線程佇列,請參閱 ConcurrentQueue<T> 。
意見反應
提交並檢視相關的意見反應