Queue<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对象的先入先出集合。
generic <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollectiongeneric <typename T>
public ref class Queue : System::Collections::Generic::IEnumerable<T>, System::Collections::ICollectionpublic 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.ICollectionpublic class Queue<T> : System.Collections.Generic.IEnumerable<T>, System.Collections.ICollectiontype 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 IEnumerablePublic 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 方法用于创建数组并将队列元素复制到其中,然后将该数组传递给采用 IEnumerable<T>的 Queue<T> 构造函数,从而创建队列的副本。 将显示副本的元素。
创建队列大小的两倍的数组,CopyTo 方法用于复制数组中间开始的数组元素。 Queue<T> 构造函数再次用于创建队列的第二个副本,其中包含开头的三个 null 元素。
Contains 方法用于显示字符串“四”位于队列的第一个副本中,之后 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> 类的新实例,该实例为空且具有指定的初始容量。 | 
属性
| Capacity | 获取内部数据结构可以保留的元素总数,而无需调整大小。 | 
| 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() | 将容量设置为 Queue<T>中实际元素数(如果该数字小于当前容量的 90%)。 | 
| TrimExcess(Int32) | 将 Queue<T> 对象的容量设置为指定的条目数。 | 
| TryDequeue(T) | 删除 Queue<T>开头的对象,并将其复制到  | 
| TryPeek(T) | 返回一个值,该值指示 Queue<T>开头是否存在对象,如果存在对象,则将其复制到  | 
显式接口实现
| ICollection.CopyTo(Array, Int32) | 从特定 Array 索引开始,将 ICollection 的元素复制到 Array。 | 
| ICollection.IsSynchronized | 获取一个值,该值指示是否同步对 ICollection 的访问(线程安全)。 | 
| ICollection.SyncRoot | 获取可用于同步对 ICollection的访问的对象。 | 
| IEnumerable.GetEnumerator() | 返回循环访问集合的枚举器。 | 
| IEnumerable<T>.GetEnumerator() | 返回循环访问集合的枚举器。 | 
扩展方法
适用于
线程安全性
此类型的公共静态(Shared)成员是线程安全的。 不保证任何实例成员都是线程安全的。
只要集合未修改,Queue<T> 就可以同时支持多个读取器。 即便如此,通过集合进行枚举本质上不是线程安全的过程。 有关线程安全的队列,请参阅 ConcurrentQueue<T>。