Queue.Dequeue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
移除並傳回在 Queue 開頭的物件。
public:
virtual System::Object ^ Dequeue();
public virtual object Dequeue ();
public virtual object? Dequeue ();
abstract member Dequeue : unit -> obj
override this.Dequeue : unit -> obj
Public Overridable Function Dequeue () As Object
傳回
從 Queue 的開頭移除的物件。
例外狀況
範例
下列範例示範如何將專案 Queue加入 、從 Queue中移除專案,或檢視 開頭的專案 Queue。
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
*/
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
*/
Imports System.Collections
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
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.
'
' 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
備註
這個方法與 Peek 方法類似,但是 Peek 不會修改 Queue。
null
可以新增至 Queue 作為值。 若要區別 Null 值和 結尾 Queue,請檢查 Count 屬性或攔截 InvalidOperationException,當 為空白時 Queue 擲回。
這個方法是作業 O(1)
。