Queue 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对象的先入先出集合。
public ref class Queue : System::Collections::ICollection
public ref class Queue : ICloneable, System::Collections::ICollection
public class Queue : 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
public class Queue : ICloneable, System.Collections.ICollection
type Queue = class
interface ICollection
interface IEnumerable
[<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
type Queue = class
interface ICollection
interface IEnumerable
interface ICloneable
Public Class Queue
Implements ICollection
Public Class Queue
Implements ICloneable, ICollection
- 继承
-
Queue
- 属性
- 实现
示例
以下示例演示如何创建值并将其添加到其中 Queue 以及如何输出其值。
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 !
注解
此类将队列实现为循环数组。 存储在 a Queue 对象中的对象插入一端,并从另一端删除。
需要临时存储以获取信息时,队列和堆栈非常有用;也就是说,在检索元素值后可能要放弃该元素时。 如果需要按照存储在集合中的相同顺序访问信息,请使用 Queue 。 如果需要按相反顺序访问信息,请使用 Stack 。 使用 ConcurrentQueue<T> 或 ConcurrentStack<T> 如果需要同时从多个线程访问集合。
可以对一个 Queue 及其元素执行三个主要操作:
一个 Queue 元素的容量是可以保留的元素 Queue 数。 随着元素添加到 a Queue,容量通过重新分配自动增加。 可以通过调用 TrimToSize来减少容量。
增长因子是当需要更大的容量时,当前容量乘以的数字。 构造增长因子时 Queue 确定。 默认增长因子为 2.0。 无论增长因素如何,该 Queue 容量将始终至少增加四个。 例如,当需要更大的容量时, Queue 总增长系数为 1.0 的容量将始终增加 4。
Queue 接受 null 为有效值并允许重复元素。
有关此集合的通用版本,请参阅 System.Collections.Generic.Queue<T>
构造函数
| 名称 | 说明 |
|---|---|
| Queue() |
初始化为空的 Queue 类的新实例,具有默认的初始容量,并使用默认增长因子。 |
| Queue(ICollection) |
初始化包含从指定集合复制的元素的类的新实例 Queue ,其初始容量与复制的元素数相同,并使用默认增长因子。 |
| Queue(Int32, Single) |
初始化为空的 Queue 类的新实例,具有指定的初始容量,并使用指定的增长因子。 |
| Queue(Int32) |
初始化类的新实例,该实例 Queue 为空,具有指定的初始容量,并使用默认增长因子。 |
属性
| 名称 | 说明 |
|---|---|
| Count |
获取包含在 . 中的 Queue元素数。 |
| IsSynchronized |
获取一个值,该值指示是否同步对 Queue 的访问(线程安全)。 |
| SyncRoot |
获取可用于同步对 . Queue的访问的对象。 |
方法
| 名称 | 说明 |
|---|---|
| Clear() |
从 Queue.. 中删除所有对象 |
| Clone() |
创建浅 Queue表副本。 |
| Contains(Object) |
确定元素是否在 Queue. |
| CopyTo(Array, Int32) | |
| 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的实际元素数。 |
扩展方法
| 名称 | 说明 |
|---|---|
| AsParallel(IEnumerable) |
启用查询的并行化。 |
| AsQueryable(IEnumerable) |
将 IEnumerable 转换为 IQueryable。 |
| Cast<TResult>(IEnumerable) |
将 IEnumerable 的元素强制转换为指定类型。 |
| OfType<TResult>(IEnumerable) |
根据指定类型筛选 IEnumerable 的元素。 |
适用于
线程安全性
此类型的公共静态(Shared 在 Visual Basic 中)成员是线程安全的。 不能保证任何实例成员是线程安全的。
若要保证线程安全性 Queue,必须通过方法返回 Synchronized(Queue) 的包装器执行所有操作。
通过集合进行枚举本质上不是线程安全的过程。 即使集合同步,其他线程仍可以修改集合,这会导致枚举器引发异常。 若要保证枚举期间的线程安全性,可以在整个枚举期间锁定集合,也可以捕获由其他线程所做的更改导致的异常。