Stack 類別

定義

代表物件的簡單後進先出 (LIFO) 非泛型集合。

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
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Stack = class
    interface ICollection
    interface ICloneable
    interface IEnumerable
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上使用非泛型集合

如果 Count 小於堆疊的容量, Push 則為 O(1) 作業。 如果需要增加容量以容納新元素, Push 就會變成 O(n) 作業,其中 nCountPopO(1)是作業。

Stack 接受 null 為有效值,並允許重複的專案。

建構函式

Stack()

初始化 Stack 類別的新執行個體,這個執行個體為空白且具有預設的初始容量。

Stack(ICollection)

初始化 Stack 類別的新執行個體,其含有從指定的集合複製過來的元素且具有與複製的元素數一樣的初始容量。

Stack(Int32)

初始化 Stack 類別的新執行個體,其為空白且具有指定的初始容量或預設的初始容量 (兩者中較大的那一個)。

屬性

Count

取得 Stack 中所包含的項目數。

IsSynchronized

取得值,這個值表示對 Stack 的存取是否同步 (安全執行緒)。

SyncRoot

取得可用以同步存取 Stack 的物件。

方法

Clear()

Stack 移除所有物件。

Clone()

建立 Stack 的淺層複本。

Contains(Object)

判斷某項目是否在 Stack 中。

CopyTo(Array, Int32)

Stack 複製到現有的一維 Array 中,從指定的陣列索引開始。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

傳回 IEnumeratorStack

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)

IEnumerable 轉換成 IQueryable

適用於

執行緒安全性

此類型Visual Basic) 成員中的公用靜態 (Shared 是安全線程。 並非所有的執行個體成員都是安全執行緒。

若要保證 的 Stack 執行緒安全性,所有作業都必須透過 方法傳 Synchronized(Stack) 回的包裝函式來完成。

透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱