注意:此类在 .NET Framework 2.0 版中是新增的。
表示对象的先进先出集合。
命名空间:System.Collections.Generic
程序集:System(在 system.dll 中)
<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class Queue(Of T)
Implements IEnumerable(Of T), ICollection, _
IEnumerable
Dim instance As Queue(Of T)
[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Queue<T> : IEnumerable<T>, ICollection,
IEnumerable
[SerializableAttribute]
[ComVisibleAttribute(false)]
generic<typename T>
public ref class Queue : IEnumerable<T>, ICollection,
IEnumerable
J# 支持使用泛型类型和方法,但不支持进行新的声明。
类型参数
- T
指定队列中元素的类型。
队列在按接收顺序存储消息方面非常有用,以便于进行顺序处理。存储在 Queue 中的对象在一端插入,从另一端移除。
Queue 的容量是指 Queue 可以保存的元素数。在此实现中,Queue 的默认初始容量为 8;但此默认值可能在 .NET Framework SDK 的未来版本中更改。当向 Queue 添加元素时,将通过重新分配内部数组,根据需要自动增大容量。可通过调用 TrimExcess 来减少容量。
Queue 接受 空引用(在 Visual Basic 中为 Nothing) 作为引用类型的有效值并且允许有重复的元素。
下面的代码示例演示了 Queue 泛型类的几个方法。此代码示例创建一个具有默认容量的字符串队列,并使用 Enqueue 方法对五个字符串进行排队。枚举队列元素,这不会更改队列的状态。使用 Dequeue 方法使第一个字符串出列。使用 Peek 方法查找队列中的下一项,然后使用 Dequeue 方法使该项出列。
使用 ToArray 方法创建一个数组并将队列元素复制到该数组,然后将该数组传递给接受 IEnumerable 的 Queue 构造函数以创建队列副本。将显示副本的元素。
创建一个大小是队列大小两倍的数组,并使用 CopyTo 方法从数组中间开始复制数组元素。再次使用 Queue 构造函数创建第二个队列副本,该队列在开始处包含三个空元素。
使用 Contains 方法显示字符串“four”在第一个队列副本中,然后使用 Clear 方法清除此副本并由 Count 属性显示该队列为空。
Imports System
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
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 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
*/
System.Object
System.Collections.Generic.Queue
此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。
只要不修改该集合,Queue 就可以同时支持多个阅读器。即便如此,从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
.NET Framework
受以下版本支持:2.0
.NET Compact Framework
受以下版本支持:2.0