Queue<T> 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 Queue<T> 类的新实例。
重载
Queue<T>() |
初始化 Queue<T> 类的新实例,该实例为空并且具有默认初始容量。 |
Queue<T>(IEnumerable<T>) |
初始化 Queue<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。 |
Queue<T>(Int32) |
初始化 Queue<T> 类的新实例,该实例为空并且具有指定的初始容量。 |
示例
下面的代码示例演示了此构造函数和泛型类的其他 Queue<T> 几种方法。 代码示例创建具有默认容量的字符串队列,并使用 Enqueue 该方法对五个字符串进行排队。 枚举队列的元素,不会更改队列的状态。 该方法 Dequeue 用于取消第一个字符串的排队。 该方法 Peek 用于查看队列中的下一项,然后使用 Dequeue 该方法取消排队。
该方法 ToArray 用于创建数组并将队列元素复制到该数组,然后将该数组传递给 Queue<T> 采用 IEnumerable<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
*/
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>(IEnumerable<T>)
初始化 Queue<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。
public:
Queue(System::Collections::Generic::IEnumerable<T> ^ collection);
public Queue (System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.Queue<'T> : seq<'T> -> System.Collections.Generic.Queue<'T>
Public Sub New (collection As IEnumerable(Of T))
参数
- collection
- IEnumerable<T>
其元素被复制到新的 Queue<T> 中的集合。
例外
collection
为 null
。
注解
a Queue<T> 的容量是可以容纳的元素 Queue<T> 数。 随着元素添加到 a Queue<T>,通过重新分配内部数组,容量会自动增加。
如果可以估计集合的大小,则指定初始容量无需在向集合中添加元素 Queue<T>时执行大量大小调整操作。
可以通过调用 TrimExcess来减少容量。
元素按集合读取IEnumerator<T>的顺序复制到Queue<T>该元素上。
此构造函数是 O (n
) 操作,其中 n
元素 collection
数。
适用于
Queue<T>(Int32)
初始化 Queue<T> 类的新实例,该实例为空并且具有指定的初始容量。
public:
Queue(int capacity);
public Queue (int capacity);
new System.Collections.Generic.Queue<'T> : int -> System.Collections.Generic.Queue<'T>
Public Sub New (capacity As Integer)
参数
例外
capacity
小于零。
注解
a Queue<T> 的容量是可以容纳的元素 Queue<T> 数。 随着元素添加到 a Queue<T>,通过重新分配内部数组,容量会自动增加。
如果可以估计集合的大小,则指定初始容量无需在向集合中添加元素 Queue<T>时执行大量大小调整操作。
可以通过调用 TrimExcess来减少容量。
此构造函数是 O (n
) 操作,其中n
。capacity