Stack<T> Konstruktoren
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Initialisiert eine neue Instanz der Stack<T>-Klasse.
Überlädt
| Name | Beschreibung |
|---|---|
| Stack<T>() |
Initialisiert eine neue Instanz der Klasse, die Stack<T> leer ist und über die Standardkapazität verfügt. |
| Stack<T>(IEnumerable<T>) |
Initialisiert eine neue Instanz der Stack<T> Klasse, die Elemente enthält, die aus der angegebenen Auflistung kopiert wurden, und verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente zu berücksichtigen. |
| Stack<T>(Int32) |
Initialisiert eine neue Instanz der Klasse, die Stack<T> leer ist und über die angegebene Anfangskapazität oder die Standard-Anfangskapazität verfügt, je nachdem, welcher Wert größer ist. |
Stack<T>()
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
Initialisiert eine neue Instanz der Klasse, die Stack<T> leer ist und über die Standardkapazität verfügt.
public:
Stack();
public Stack();
Public Sub New ()
Beispiele
Im folgenden Codebeispiel wird dieser Konstruktor und mehrere Methoden der Stack<T> generischen Klasse veranschaulicht.
Das Codebeispiel erstellt einen Stapel von Zeichenfolgen mit Standardkapazität und verwendet die Push Methode, um fünf Zeichenfolgen auf den Stapel zu übertragen. Die Elemente des Stapels werden aufgezählt, wodurch der Zustand des Stapels nicht geändert wird. Die Pop Methode wird verwendet, um die erste Zeichenfolge aus dem Stapel zu füllen. Die Peek Methode wird verwendet, um das nächste Element im Stapel zu betrachten, und dann wird die Pop Methode verwendet, um es auszuschalten.
Die ToArray Methode wird verwendet, um ein Array zu erstellen und die Stapelelemente darin zu kopieren. Anschließend wird das Array an den Stack<T> Konstruktor IEnumerable<T>übergeben, der eine Kopie des Stapels mit der Reihenfolge der umgekehrten Elemente erstellt. Die Elemente der Kopie werden angezeigt.
Ein Array wird doppelt so groß wie der Stapel erstellt, und die CopyTo Methode wird verwendet, um die Arrayelemente zu kopieren, beginnend mit der Mitte des Arrays. Der Stack<T> Konstruktor wird erneut verwendet, um eine Kopie des Stapels mit der Reihenfolge der umgekehrten Elemente zu erstellen. Daher befinden sich die drei NULL-Elemente am Ende.
Die Contains Methode wird verwendet, um anzuzeigen, dass sich die Zeichenfolge "four" in der ersten Kopie des Stapels befindet, nach der die Methode die Clear Kopie löscht, und die Count Eigenschaft zeigt an, dass der Stapel leer ist.
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
// A stack can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nPopping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in stack2 )
{
Console.WriteLine(number);
}
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in stack3 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nstack2.Contains(\"four\") = {0}",
stack2.Contains("four"));
Console.WriteLine("\nstack2.Clear()");
stack2.Clear();
Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
}
}
/* This code example produces the following output:
five
four
three
two
one
Popping 'five'
Peek at next item to destack: four
Popping 'four'
Contents of the first copy:
one
two
three
Contents of the second copy, with duplicates and nulls:
one
two
three
stack2.Contains("four") = False
stack2.Clear()
stack2.Count = 0
*/
open System
open System.Collections.Generic
let numbers = Stack()
numbers.Push "one"
numbers.Push "two"
numbers.Push "three"
numbers.Push "four"
numbers.Push "five"
// A stack can be enumerated without disturbing its contents.
for number in numbers do
printfn $"{number}"
printfn $"\nPopping '{numbers.Pop()}'"
printfn $"Peek at next item to destack: {numbers.Peek()}"
numbers.Peek() |> ignore
printfn $"Popping '{numbers.Pop()}'"
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
let stack2 = numbers.ToArray() |> Stack
printfn "\nContents of the first copy:"
for number in stack2 do
printfn $"{number}"
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
let array2 = numbers.Count * 2 |> Array.zeroCreate
numbers.CopyTo(array2, numbers.Count)
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
let stack3 = Stack array2
printfn "\nContents of the second copy, with duplicates and nulls:"
for number in stack3 do
printfn $"{number}"
printfn
$"""
stack2.Contains "four" = {stack2.Contains "four"}"""
printfn "\nstack2.Clear()"
stack2.Clear()
printfn $"\nstack2.Count = {stack2.Count}"
// This code example produces the following output:
// five
// four
// three
// two
// one
//
// Popping 'five'
// Peek at next item to destack: four
// Popping 'four'
//
// Contents of the first copy:
// one
// two
// three
//
// Contents of the second copy, with duplicates and nulls:
// one
// two
// three
//
// stack2.Contains("four") = False
//
// stack2.Clear()
//
// stack2.Count = 0
Imports System.Collections.Generic
Module Example
Sub Main
Dim numbers As New Stack(Of String)
numbers.Push("one")
numbers.Push("two")
numbers.Push("three")
numbers.Push("four")
numbers.Push("five")
' A stack can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop())
Console.WriteLine("Peek at next item to pop: {0}", _
numbers.Peek())
Console.WriteLine("Popping '{0}'", numbers.Pop())
' Create another stack, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T). Note that
' the order of items on the new stack is reversed.
Dim stack2 As New Stack(Of String)(numbers.ToArray())
Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In stack2
Console.WriteLine(number)
Next
' Create an array twice the size of the stack, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the stack, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)
' Create a second stack, using the constructor that accepts an
' IEnumerable(Of T). The elements are reversed, with the null
' elements appearing at the end of the stack when enumerated.
Dim stack3 As New Stack(Of String)(array2)
Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In stack3
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _
stack2.Contains("four"))
Console.WriteLine(vbLf & "stack2.Clear()")
stack2.Clear()
Console.WriteLine(vbLf & "stack2.Count = {0}", _
stack2.Count)
End Sub
End Module
' This code example produces the following output:
'
'five
'four
'three
'two
'one
'
'Popping 'five'
'Peek at next item to pop: four
'Popping 'four'
'
'Contents of the first copy:
'one
'two
'three
'
'Contents of the second copy, with duplicates and nulls:
'one
'two
'three
'
'
'
'
'stack2.Contains("four") = False
'
'stack2.Clear()
'
'stack2.Count = 0
Hinweise
Die Kapazität eines Elements Stack<T> ist die Anzahl der Elemente, die aufbewahrt Stack<T> werden können. Wenn Elemente zu einem Stack<T>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden Stack<T>.
Die Kapazität kann durch Aufrufe TrimExcessverringert werden.
Dieser Konstruktor ist ein O(1)-Vorgang.
Gilt für:
Stack<T>(IEnumerable<T>)
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
Initialisiert eine neue Instanz der Stack<T> Klasse, die Elemente enthält, die aus der angegebenen Auflistung kopiert wurden, und verfügt über ausreichende Kapazität, um die Anzahl der kopierten Elemente zu berücksichtigen.
public:
Stack(System::Collections::Generic::IEnumerable<T> ^ collection);
public Stack(System.Collections.Generic.IEnumerable<T> collection);
new System.Collections.Generic.Stack<'T> : seq<'T> -> System.Collections.Generic.Stack<'T>
Public Sub New (collection As IEnumerable(Of T))
Parameter
- collection
- IEnumerable<T>
Die Auflistung, aus der Elemente kopiert werden sollen.
Ausnahmen
collection ist null.
Beispiele
Im folgenden Codebeispiel wird dieser Konstruktor und mehrere Methoden der Stack<T> generischen Klasse veranschaulicht.
Das Codebeispiel erstellt einen Stapel von Zeichenfolgen mit Standardkapazität und verwendet die Push Methode, um fünf Zeichenfolgen auf den Stapel zu übertragen. Die Elemente des Stapels werden aufgezählt, wodurch der Zustand des Stapels nicht geändert wird. Die Pop Methode wird verwendet, um die erste Zeichenfolge aus dem Stapel zu füllen. Die Peek Methode wird verwendet, um das nächste Element im Stapel zu betrachten, und dann wird die Pop Methode verwendet, um es auszuschalten.
Die ToArray Methode wird verwendet, um ein Array zu erstellen und die Stapelelemente darin zu kopieren. Anschließend wird das Array an den Stack<T> Konstruktor IEnumerable<T>übergeben, der eine Kopie des Stapels mit der Reihenfolge der umgekehrten Elemente erstellt. Die Elemente der Kopie werden angezeigt.
Ein Array wird doppelt so groß wie der Stapel erstellt, und die CopyTo Methode wird verwendet, um die Arrayelemente zu kopieren, beginnend mit der Mitte des Arrays. Der Stack<T> Konstruktor wird erneut verwendet, um eine Kopie des Stapels mit der Reihenfolge der umgekehrten Elemente zu erstellen. Daher befinden sich die drei NULL-Elemente am Ende.
Die Contains Methode wird verwendet, um anzuzeigen, dass sich die Zeichenfolge "four" in der ersten Kopie des Stapels befindet, nach der die Methode die Clear Kopie löscht, und die Count Eigenschaft zeigt an, dass der Stapel leer ist.
using System;
using System.Collections.Generic;
class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");
// A stack can be enumerated without disturbing its contents.
foreach( string number in numbers )
{
Console.WriteLine(number);
}
Console.WriteLine("\nPopping '{0}'", numbers.Pop());
Console.WriteLine("Peek at next item to destack: {0}",
numbers.Peek());
Console.WriteLine("Popping '{0}'", numbers.Pop());
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
Stack<string> stack2 = new Stack<string>(numbers.ToArray());
Console.WriteLine("\nContents of the first copy:");
foreach( string number in stack2 )
{
Console.WriteLine(number);
}
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
string[] array2 = new string[numbers.Count * 2];
numbers.CopyTo(array2, numbers.Count);
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
Stack<string> stack3 = new Stack<string>(array2);
Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");
foreach( string number in stack3 )
{
Console.WriteLine(number);
}
Console.WriteLine("\nstack2.Contains(\"four\") = {0}",
stack2.Contains("four"));
Console.WriteLine("\nstack2.Clear()");
stack2.Clear();
Console.WriteLine("\nstack2.Count = {0}", stack2.Count);
}
}
/* This code example produces the following output:
five
four
three
two
one
Popping 'five'
Peek at next item to destack: four
Popping 'four'
Contents of the first copy:
one
two
three
Contents of the second copy, with duplicates and nulls:
one
two
three
stack2.Contains("four") = False
stack2.Clear()
stack2.Count = 0
*/
open System
open System.Collections.Generic
let numbers = Stack()
numbers.Push "one"
numbers.Push "two"
numbers.Push "three"
numbers.Push "four"
numbers.Push "five"
// A stack can be enumerated without disturbing its contents.
for number in numbers do
printfn $"{number}"
printfn $"\nPopping '{numbers.Pop()}'"
printfn $"Peek at next item to destack: {numbers.Peek()}"
numbers.Peek() |> ignore
printfn $"Popping '{numbers.Pop()}'"
// Create a copy of the stack, using the ToArray method and the
// constructor that accepts an IEnumerable<T>.
let stack2 = numbers.ToArray() |> Stack
printfn "\nContents of the first copy:"
for number in stack2 do
printfn $"{number}"
// Create an array twice the size of the stack and copy the
// elements of the stack, starting at the middle of the
// array.
let array2 = numbers.Count * 2 |> Array.zeroCreate
numbers.CopyTo(array2, numbers.Count)
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
let stack3 = Stack array2
printfn "\nContents of the second copy, with duplicates and nulls:"
for number in stack3 do
printfn $"{number}"
printfn
$"""
stack2.Contains "four" = {stack2.Contains "four"}"""
printfn "\nstack2.Clear()"
stack2.Clear()
printfn $"\nstack2.Count = {stack2.Count}"
// This code example produces the following output:
// five
// four
// three
// two
// one
//
// Popping 'five'
// Peek at next item to destack: four
// Popping 'four'
//
// Contents of the first copy:
// one
// two
// three
//
// Contents of the second copy, with duplicates and nulls:
// one
// two
// three
//
// stack2.Contains("four") = False
//
// stack2.Clear()
//
// stack2.Count = 0
Imports System.Collections.Generic
Module Example
Sub Main
Dim numbers As New Stack(Of String)
numbers.Push("one")
numbers.Push("two")
numbers.Push("three")
numbers.Push("four")
numbers.Push("five")
' A stack can be enumerated without disturbing its contents.
For Each number As String In numbers
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "Popping '{0}'", numbers.Pop())
Console.WriteLine("Peek at next item to pop: {0}", _
numbers.Peek())
Console.WriteLine("Popping '{0}'", numbers.Pop())
' Create another stack, using the ToArray method and the
' constructor that accepts an IEnumerable(Of T). Note that
' the order of items on the new stack is reversed.
Dim stack2 As New Stack(Of String)(numbers.ToArray())
Console.WriteLine(vbLf & "Contents of the first copy:")
For Each number As String In stack2
Console.WriteLine(number)
Next
' Create an array twice the size of the stack, compensating
' for the fact that Visual Basic allocates an extra array
' element. Copy the elements of the stack, starting at the
' middle of the array.
Dim array2((numbers.Count * 2) - 1) As String
numbers.CopyTo(array2, numbers.Count)
' Create a second stack, using the constructor that accepts an
' IEnumerable(Of T). The elements are reversed, with the null
' elements appearing at the end of the stack when enumerated.
Dim stack3 As New Stack(Of String)(array2)
Console.WriteLine(vbLf & _
"Contents of the second copy, with duplicates and nulls:")
For Each number As String In stack3
Console.WriteLine(number)
Next
Console.WriteLine(vbLf & "stack2.Contains(""four"") = {0}", _
stack2.Contains("four"))
Console.WriteLine(vbLf & "stack2.Clear()")
stack2.Clear()
Console.WriteLine(vbLf & "stack2.Count = {0}", _
stack2.Count)
End Sub
End Module
' This code example produces the following output:
'
'five
'four
'three
'two
'one
'
'Popping 'five'
'Peek at next item to pop: four
'Popping 'four'
'
'Contents of the first copy:
'one
'two
'three
'
'Contents of the second copy, with duplicates and nulls:
'one
'two
'three
'
'
'
'
'stack2.Contains("four") = False
'
'stack2.Clear()
'
'stack2.Count = 0
Hinweise
Die Kapazität eines Elements Stack<T> ist die Anzahl der Elemente, die aufbewahrt Stack<T> werden können. Wenn Elemente zu einem Stack<T>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden Stack<T>.
Die Kapazität kann durch Aufrufe TrimExcessverringert werden.
Die Elemente werden in die Stack<T> gleiche Reihenfolge kopiert, in der sie von IEnumerator<T> der Auflistung gelesen werden.
Dieser Konstruktor ist ein O(n)-Vorgang. n Dabei handelt es sich um die Anzahl der Elemente in collection.
Weitere Informationen
Gilt für:
Stack<T>(Int32)
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
- Quelle:
- Stack.cs
Initialisiert eine neue Instanz der Klasse, die Stack<T> leer ist und über die angegebene Anfangskapazität oder die Standard-Anfangskapazität verfügt, je nachdem, welcher Wert größer ist.
public:
Stack(int capacity);
public Stack(int capacity);
new System.Collections.Generic.Stack<'T> : int -> System.Collections.Generic.Stack<'T>
Public Sub New (capacity As Integer)
Parameter
Ausnahmen
capacity ist kleiner als 0 (null).
Hinweise
Die Kapazität eines Elements Stack<T> ist die Anzahl der Elemente, die aufbewahrt Stack<T> werden können. Wenn Elemente zu einem Stack<T>Element hinzugefügt werden, wird die Kapazität bei Bedarf automatisch erhöht, indem das interne Array neu zugeordnet wird.
Wenn die Größe der Auflistung geschätzt werden kann, beseitigt die Angabe der anfänglichen Kapazität die Notwendigkeit, eine Reihe von Größenänderungsvorgängen auszuführen, während Elemente hinzugefügt werden Stack<T>.
Die Kapazität kann durch Aufrufe TrimExcessverringert werden.
Dieser Konstruktor ist ein O()-Vorgang, wobei n es sich dabei um einen O(n)-Vorgang handeltcapacity.