Queue.Enqueue(Object) 方法

定义

将对象添加到 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)

参数

obj
Object

要添加到 Queue 的对象。 该值可以为 null

示例

以下示例演示如何将 元素添加到 、 QueueQueue中删除元素或查看 的开头的 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

注解

Queue 容量是 可以容纳的元素 Queue 数。 将元素添加到 时 Queue,容量会根据需要通过重新分配自动增加。 可以通过调用 TrimToSize来降低容量。

增长因子是当需要更大的容量时,当前容量乘以的数字。 增长因子是在构造 时 Queue 确定的。 的容量 Queue 始终以最小值增加,而不管增长因子如何;增长因子 1.0 不会阻止 Queue 大小增加。

如果 Count 小于内部数组的容量,则此方法是一种 O(1) 操作。 如果需要重新分配内部数组以适应新元素,此方法将成为一个 O(n) 操作,其中 nCount

适用于

另请参阅