Stack-Klasse
Stellt eine einfache nicht generische LIFO (Last-In-First-Out)-Auflistung von Objekten dar.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class Stack
Implements ICollection, IEnumerable, ICloneable
'Usage
Dim instance As Stack
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class Stack : ICollection, IEnumerable, ICloneable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class Stack : ICollection, IEnumerable, ICloneable
/** @attribute SerializableAttribute() */
/** @attribute ComVisibleAttribute(true) */
public class Stack implements ICollection, IEnumerable,
ICloneable
SerializableAttribute
ComVisibleAttribute(true)
public class Stack implements ICollection, IEnumerable,
ICloneable
Hinweise
Die generische Version dieser Auflistung finden Sie unter System.Collections.Generic.Stack.
Stack wird als zirkulärer Puffer implementiert.
Bei der Kapazität eines Stack handelt es sich um die Anzahl der Elemente, die die Stack enthalten kann. Die anfängliche Standardkapazität für einen Stack ist 10. Beim Hinzufügen von Elementen zum Stack wird die Kapazität durch Neureservierung automatisch nach Bedarf erhöht.
Wenn Count kleiner als die Kapazität des Stapels ist, ist Push eine O(1)-Operation. Wenn die Kapazität zur Anpassung an das neue Element erhöht werden muss, wird Push zu einer O(n)-Operation, wobei n gleich Count ist. Pop ist eine O(1)-Operation.
Stack akzeptiert NULL (Nothing in Visual Basic) als gültigen Wert und lässt doppelte Elemente zu.
Beispiel
Im folgenden Beispiel wird das Erstellen und Hinzufügen von Werten für Stack sowie die Ausgabe der enthaltenen Werte dargestellt.
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
*/
Vererbungshierarchie
System.Object
System.Collections.Stack
Microsoft.VisualC.SymbolTableStack
Threadsicherheit
Öffentliche statische (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Zur Gewährleistung der Threadsicherheit von Stack müssen alle Operationen unter Verwendung des Wrappers ausgeführt werden, der von der Synchronized-Methode zurückgegeben wird.
Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert ist, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Sie können während der Enumeration Threadsicherheit gewährleisten, indem Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen abfangen, die durch Änderungen ausgelöst werden, die von anderen Threads vorgenommen werden.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
Stack-Member
System.Collections-Namespace
System.Collections.Generic.Stack