다음을 통해 공유


Stack.Clear 메서드

정의

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

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

예제

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

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

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.
 
 Initially,
    Count    : 5
    Values:    jumps    fox    brown    quick    The
 After Clear,
    Count    : 0
    Values:
 */
using System;
using System.Collections;

public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push( "The" );
      myStack.Push( "quick" );
      myStack.Push( "brown" );
      myStack.Push( "fox" );
      myStack.Push( "jumps" );

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

      // Clears the Stack.
      myStack.Clear();

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

   public static void PrintValues( IEnumerable myCollection )  {
      foreach ( Object obj in myCollection )
         Console.Write( "    {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Initially,
   Count    : 5
   Values:    jumps    fox    brown    quick    The
After Clear,
   Count    : 0
   Values:
*/
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        myStack.Push("jumps")
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("Initially,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)
        
        ' Clears the Stack.
        myStack.Clear()
        
        ' Displays the count and values of the Stack.
        Console.WriteLine("After Clear,")
        Console.WriteLine("   Count    : {0}", myStack.Count)
        Console.Write("   Values:")
        PrintValues(myStack)

    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.
' 
' Initially,
'    Count    : 5
'    Values:    jumps    fox    brown    quick    The
' After Clear,
'    Count    : 0
'    Values:

설명

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

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

적용 대상