Queue.Enqueue(Object) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menambahkan objek ke akhir Queue.
public:
virtual void Enqueue(System::Object ^ obj);
public virtual void Enqueue (object obj);
public virtual void Enqueue (object? obj);
abstract member Enqueue : obj -> unit
override this.Enqueue : obj -> unit
Public Overridable Sub Enqueue (obj As Object)
Parameter
Contoh
Contoh berikut menunjukkan cara menambahkan elemen ke Queue, menghapus elemen dari Queue, atau melihat elemen di awal 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
Keterangan
Kapasitas adalah Queue jumlah elemen yang dapat ditahan Queue . Karena elemen ditambahkan ke Queue, kapasitas secara otomatis ditingkatkan sesuai kebutuhan melalui realokasi. Kapasitas dapat dikurangi dengan memanggil TrimToSize.
Faktor pertumbuhan adalah jumlah di mana kapasitas saat ini dikalikan ketika kapasitas yang lebih besar diperlukan. Faktor pertumbuhan ditentukan ketika Queue dibangun. Kapasitas Queue akan selalu meningkat dengan nilai minimum, terlepas dari faktor pertumbuhan; faktor pertumbuhan 1,0 tidak akan mencegah Queue peningkatan ukuran.
Jika Count kurang dari kapasitas array internal, metode ini adalah O(1)
operasi. Jika array internal perlu di-realokasi untuk mengakomodasi elemen baru, metode ini menjadi O(n)
operasi, di mana n
adalah Count.