Stack クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
<SerializableAttribute> _ <ComVisibleAttribute(True)> _ Public Class Stack Implements ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public class Stack : ICollection, IEnumerable, ICloneable
[SerializableAttribute] [ComVisibleAttribute(true)] public ref class Stack : ICollection, IEnumerable, ICloneable
このコレクションのジェネリック バージョンについては、「System.Collections.Generic.Stack」を参照してください。
Stack の容量は、Stack が保持できる要素数になります。Stack の既定の初期量は 10 です。Stack に要素を追加すると、必要に応じて、再割り当てによって容量が自動的に増加します。
Count がスタックの容量よりも小さい場合、Push は O(1) 操作になります。新しい要素を格納するために容量を増やす必要がある場合、Push は O(n) 操作になります。ここで、n は Count です。Pop は O(1) 操作です。
Stack は、null 参照 (Visual Basic では Nothing) を有効な値として受け取り、要素の重複を許可します。
Stack を作成して値を追加する方法と、その値を出力する方法の例を次に示します。
Imports System Imports System.Collections Imports Microsoft.VisualBasic 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 'PrintValues End Class ' 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 */
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 */
import System.*; import System.Collections.*; public class SamplesStack { public static void main(String[] args) { // 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}", System.Convert.ToString(myStack.get_Count())); Console.Write("\tValues:"); PrintValues(myStack); } //main public static void PrintValues(IEnumerable myCollection) { IEnumerator objEnum = myCollection.GetEnumerator(); while (objEnum.MoveNext()) { Console.Write(" {0}", objEnum.get_Current()); } Console.WriteLine(); } //PrintValues } //SamplesStack /* This code produces the following output. myStack Count: 3 Values: ! World Hello */
System.Collections.Stack
Microsoft.VisualC.SymbolTableStack
この型の public static (Visual Basic では Shared) メンバは、スレッド セーフです。すべてのインスタンス メンバがスレッド セーフになるかどうかは保証されていません。
Stack を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- Stack クラスのページへのリンク