다음을 통해 공유


Queue.Clear 메서드

정의

Queue에서 개체를 모두 제거합니다.

public:
 virtual void Clear();
public virtual void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Overridable Sub Clear ()

예제

다음 예제에서는 의 Queue값을 지우는 방법을 보여줍니다.

using namespace System;
using namespace System::Collections;
void PrintValues( Queue^ myQ );
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "The" );
   myQ->Enqueue( "quick" );
   myQ->Enqueue( "brown" );
   myQ->Enqueue( "fox" );
   myQ->Enqueue( "jumps" );
   
   // Displays the count and values of the Queue.
   Console::WriteLine( "Initially," );
   Console::WriteLine( "   Count    : {0}", myQ->Count );
   Console::Write( "   Values:" );
   PrintValues( myQ );
   
   // Clears the Queue.
   myQ->Clear();
   
   // Displays the count and values of the Queue.
   Console::WriteLine( "After Clear," );
   Console::WriteLine( "   Count    : {0}", myQ->Count );
   Console::Write( "   Values:" );
   PrintValues( myQ );
}

void PrintValues( Queue^ myQ )
{
   IEnumerator^ myEnum = myQ->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ myObj = safe_cast<Object^>(myEnum->Current);
      Console::Write( "    {0}", myObj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 Initially,
    Count    : 5
    Values:    The    quick    brown    fox    jumps
 After Clear,
    Count    : 0
    Values:

 */
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" );
      myQ.Enqueue( "jumps" );

      // Displays the count and values of the Queue.
      Console.WriteLine( "Initially," );
      Console.WriteLine( "   Count    : {0}", myQ.Count );
      Console.Write( "   Values:" );
      PrintValues( myQ );

      // Clears the Queue.
      myQ.Clear();

      // Displays the count and values of the Queue.
      Console.WriteLine( "After Clear," );
      Console.WriteLine( "   Count    : {0}", myQ.Count );
      Console.Write( "   Values:" );
      PrintValues( myQ );
   }

   public static void PrintValues( Queue myQ )  {
      foreach ( Object myObj in myQ )  {
         Console.Write( "    {0}", myObj );
      }
      Console.WriteLine();
   }
}
/*
This code produces the following output.

Initially,
   Count    : 5
   Values:    The    quick    brown    fox    jumps
After Clear,
   Count    : 0
   Values:

*/
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")
        myQ.Enqueue("jumps")
        
        ' Displays the count and values of the Queue.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myQ.Count)
        Console.Write("   Values:")
        PrintValues(myQ)
        
        ' Clears the Queue.
        myQ.Clear()
        
        ' Displays the count and values of the Queue.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myQ.Count)
        Console.Write("   Values:")
        PrintValues(myQ)
    End Sub
    
    Public Shared Sub PrintValues(myQ As Queue)
        Dim myObj As [Object]
        For Each myObj In  myQ
            Console.Write("    {0}", myObj)
        Next myObj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' Initially,
'    Count    : 5
'    Values:    The    quick    brown    fox    jumps
' After Clear,
'    Count    : 0
'    Values:

설명

Count가 0으로 설정되어 있고 컬렉션 요소에서 다른 개체로의 참조도 해제되어 있습니다.

용량은 변경되지 않은 상태로 유지됩니다. 의 용량을 다시 설정하려면 를 Queue호출합니다 TrimToSize. 빈 Queue 값을 트리밍하면 의 Queue 용량이 기본 용량으로 설정됩니다.

이 메서드는 작업입니다 O(n) . 여기서 n 는 입니다 Count.

적용 대상

추가 정보