Stack 클래스

정의

제네릭이 아닌 개체의 간단한 LIFO(Last In First Out: 마지막에 들어간 것부터 사용) 컬렉션을 나타냅니다.

public ref class Stack : System::Collections::ICollection
public ref class Stack : ICloneable, System::Collections::ICollection
public class Stack : System.Collections.ICollection
public class Stack : ICloneable, System.Collections.ICollection
[System.Serializable]
public class Stack : ICloneable, System.Collections.ICollection
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class Stack : ICloneable, System.Collections.ICollection
type Stack = class
    interface ICollection
    interface IEnumerable
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Stack = class
    interface ICollection
    interface IEnumerable
    interface ICloneable
Public Class Stack
Implements ICollection
Public Class Stack
Implements ICloneable, ICollection
상속
Stack
특성
구현

예제

다음 예제에서는 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( "Hello" );
   myStack->Push( "World" );
   myStack->Push( "!" );
   
   // Displays the properties and values of the Stack.
   Console::WriteLine( "myStack" );
   Console::WriteLine( "\tCount:    {0}", myStack->Count );
   Console::Write( "\tValues:" );
   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.
 
 myStack
     Count:    3
     Values:    !    World    Hello
 */
using System;
using System.Collections;
public class SamplesStack  {

   public static void Main()  {

      // Creates and initializes a new Stack.
      Stack myStack = new Stack();
      myStack.Push("Hello");
      myStack.Push("World");
      myStack.Push("!");

      // Displays the properties and values of the Stack.
      Console.WriteLine( "myStack" );
      Console.WriteLine( "\tCount:    {0}", myStack.Count );
      Console.Write( "\tValues:" );
      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.

myStack
    Count:    3
    Values:    !    World    Hello
*/
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
    
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("Hello")
        myStack.Push("World")
        myStack.Push("!")
        
        ' Displays the properties and values of the Stack.
        Console.WriteLine("myStack")
        Console.WriteLine(ControlChars.Tab & "Count:    {0}", myStack.Count)
        Console.Write(ControlChars.Tab & "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.
'
' myStack
'     Count:     3
'     Values:    !    World    Hello

설명

Stack 용량은 가 보유할 수 있는 Stack 요소의 수입니다. 요소가 에 Stack추가되면 재할당을 통해 필요에 따라 용량이 자동으로 증가합니다.

중요

새 개발에 클래스를 Stack 사용하지 않는 것이 좋습니다. 대신 제네릭 System.Collections.Generic.Stack<T> 클래스를 사용하는 것이 좋습니다. 자세한 내용은 GitHub에서 제네릭이 아닌 컬렉션을 사용하면 안 됨 을 참조하세요.

가 스택 Push 의 용량보다 작은 경우 Count 는 작업입니다O(1). 새 요소를 수용하기 위해 용량을 늘려야 하는 경우 는 Push 작업이 되고 O(n) , 여기서 n 는 입니다 Count. Pop 는 작업입니다 O(1) .

Stacknull 유효한 값으로 허용되고 중복 요소를 허용합니다.

생성자

Stack()

비어 있는 상태에서 기본 초기 용량을 가지는 Stack 클래스의 새 인스턴스를 초기화합니다.

Stack(ICollection)

지정한 컬렉션에서 복사된 요소가 포함되어 있고 복사된 요소의 수와 같은 초기 용량을 가지는 Stack 클래스의 새 인스턴스를 초기화합니다.

Stack(Int32)

비어 있는 상태이고 지정한 초기 용량과 기본 초기 용량 중에서 더 큰 용량을 가지는 Stack 클래스의 새 인스턴스를 초기화합니다.

속성

Count

Stack에 포함된 요소 수를 가져옵니다.

IsSynchronized

Stack에 대한 액세스가 동기화되어 스레드로부터 안전하게 보호되는지를 나타내는 값을 가져옵니다.

SyncRoot

Stack에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다.

메서드

Clear()

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

Clone()

Stack의 부분 복사본을 만듭니다.

Contains(Object)

Stack에 요소가 있는지 여부를 확인합니다.

CopyTo(Array, Int32)

지정된 배열 인덱스에서 시작하는 기존 1차원 ArrayStack를 복사합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetEnumerator()

IEnumerator 에 대한 Stack를 반환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
Peek()

Stack의 맨 위에서 개체를 제거하지 않고 반환합니다.

Pop()

Stack의 맨 위에서 개체를 제거하고 반환합니다.

Push(Object)

개체를 Stack의 맨 위에 삽입합니다.

Synchronized(Stack)

동기화되어 스레드로부터 안전하게 보호되는 Stack의 래퍼를 반환합니다.

ToArray()

Stack을 새 배열에 복사합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

확장 메서드

Cast<TResult>(IEnumerable)

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

OfType<TResult>(IEnumerable)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

AsParallel(IEnumerable)

쿼리를 병렬화할 수 있도록 합니다.

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상

스레드 보안

공용 정적 (Shared Visual Basic의)이 형식의 멤버는 스레드로부터 안전 합니다. 인스턴스 구성원은 스레드로부터의 안전성이 보장되지 않습니다.

Stack스레드 안전을 보장하려면 메서드에서 반환 Synchronized(Stack) 된 래퍼를 통해 모든 작업을 수행해야 합니다.

컬렉션 전체를 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.

추가 정보