Queue.Dequeue 方法
移除并返回位于 Queue 开始处的对象。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overridable Function Dequeue As Object
用法
Dim instance As Queue
Dim returnValue As Object
returnValue = instance.Dequeue
public virtual Object Dequeue ()
public:
virtual Object^ Dequeue ()
public Object Dequeue ()
public function Dequeue () : Object
返回值
从 Queue 的开头移除的对象。
异常
异常类型 | 条件 |
---|---|
Queue 为空。 |
备注
此方法类似于 Peek 方法,但 Peek 不修改 Queue。
空引用(在 Visual Basic 中为 Nothing) 可以作为值添加到 Queue。若要区分空值和 Queue 结尾,请检查 Count 属性或捕捉 Queue 为空时引发的 InvalidOperationException。
此方法的运算复杂度为 O(1)。
示例
下面的示例演示如何向 Queue 中添加元素,如何从 Queue 中移除元素或如何查看 Queue 开头的元素。
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic
Public Class SamplesQueue
Public Shared Sub Main()
' Creates and initializes a new Queue.
Dim myQ As New Queue()
myQ.Enqueue("The")
myQ.Enqueue("quick")
myQ.Enqueue("brown")
myQ.Enqueue("fox")
' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)
' Removes an element from the Queue.
Console.WriteLine("(Dequeue) {0}", myQ.Dequeue())
' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)
' Removes another element from the Queue.
Console.WriteLine("(Dequeue) {0}", myQ.Dequeue())
' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)
' Views the first element in the Queue but does not remove it.
Console.WriteLine("(Peek) {0}", myQ.Peek())
' Displays the Queue.
Console.Write("Queue values:")
PrintValues(myQ)
End Sub 'Main
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 'PrintValues
End Class 'SamplesQueue
' This code produces the following output.
'
' Queue values: The quick brown fox
' (Dequeue) The
' Queue values: quick brown fox
' (Dequeue) quick
' Queue values: brown fox
' (Peek) brown
' Queue values: brown fox
using System;
using System.Collections;
public class SamplesQueue {
public static void Main() {
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue( "The" );
myQ.Enqueue( "quick" );
myQ.Enqueue( "brown" );
myQ.Enqueue( "fox" );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Removes an element from the Queue.
Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Removes another element from the Queue.
Console.WriteLine( "(Dequeue)\t{0}", myQ.Dequeue() );
// Displays the Queue.
Console.Write( "Queue values:" );
PrintValues( myQ );
// Views the first element in the Queue but does not remove it.
Console.WriteLine( "(Peek) \t{0}", myQ.Peek() );
// Displays the Queue.
Console.Write( "Queue values:" );
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.
Queue values: The quick brown fox
(Dequeue) The
Queue values: quick brown fox
(Dequeue) quick
Queue values: brown fox
(Peek) brown
Queue values: brown fox
*/
using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myCollection );
int main()
{
// Creates and initializes a new Queue.
Queue^ myQ = gcnew Queue;
myQ->Enqueue( "The" );
myQ->Enqueue( "quick" );
myQ->Enqueue( "brown" );
myQ->Enqueue( "fox" );
// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );
// Removes an element from the Queue.
Console::WriteLine( "(Dequeue)\t{0}", myQ->Dequeue() );
// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );
// Removes another element from the Queue.
Console::WriteLine( "(Dequeue)\t{0}", myQ->Dequeue() );
// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );
// Views the first element in the Queue but does not remove it.
Console::WriteLine( "(Peek) \t{0}", myQ->Peek() );
// Displays the Queue.
Console::Write( "Queue values:" );
PrintValues( myQ );
}
void PrintValues( IEnumerable^ myCollection )
{
IEnumerator^ myEnum = myCollection->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ obj = safe_cast<Object^>(myEnum->Current);
Console::Write( " {0}", obj );
}
Console::WriteLine();
}
/*
This code produces the following output.
Queue values: The quick brown fox
(Dequeue) The
Queue values: quick brown fox
(Dequeue) quick
Queue values: brown fox
(Peek) brown
Queue values: brown fox
*/
import System.*;
import System.Collections.*;
public class SamplesQueue
{
public static void main(String[] args)
{
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue("The");
myQ.Enqueue("quick");
myQ.Enqueue("brown");
myQ.Enqueue("fox");
// Displays the Queue.
Console.Write("Queue values:");
PrintValues(myQ);
// Removes an element from the Queue.
Console.WriteLine("(Dequeue)\t{0}", myQ.Dequeue());
// Displays the Queue.
Console.Write("Queue values:");
PrintValues(myQ);
// Removes another element from the Queue.
Console.WriteLine("(Dequeue)\t{0}", myQ.Dequeue());
// Displays the Queue.
Console.Write("Queue values:");
PrintValues(myQ);
// Views the first element in the Queue but does not remove it.
Console.WriteLine("(Peek) \t{0}", myQ.Peek());
// Displays the Queue.
Console.Write("Queue values:");
PrintValues(myQ);
} //main
public static void PrintValues(IEnumerable myCollection)
{
IEnumerator e = myCollection.GetEnumerator();
while (e.MoveNext()) {
Object obj = e.get_Current();
Console.Write(" {0}", obj);
}
Console.WriteLine();
} //PrintValues
} //SamplesQueue
/*
This code produces the following output.
Queue values: The quick brown fox
(Dequeue) The
Queue values: quick brown fox
(Dequeue) quick
Queue values: brown fox
(Peek) brown
Queue values: brown fox
*/
平台
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、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0