Stack<T> 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 Stack<T> 类的新实例。
重载
Stack<T>() |
初始化 Stack<T> 类的新实例,该实例为空并且具有默认初始容量。 |
Stack<T>(IEnumerable<T>) |
初始化 Stack<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。 |
Stack<T>(Int32) |
初始化 Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。 |
Stack<T>()
- Source:
- Stack.cs
- Source:
- Stack.cs
- Source:
- Stack.cs
初始化 Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
public:
Stack();
public Stack ();
Public Sub New ()
示例
下面的代码示例演示此构造函数和泛型类的 Stack<T> 多个方法。
该代码示例创建一个具有默认容量的字符串堆栈, Push 并使用 方法将五个字符串推送到堆栈上。 枚举堆栈的元素,这不会更改堆栈的状态。 方法 Pop 用于从堆栈中弹出第一个字符串。 方法 Peek 用于查看堆栈上的下一项,然后使用 Pop 方法将其弹出。
ToArray方法用于创建一个数组并将堆栈元素复制到其中,然后将该数组传递给Stack<T>采用 IEnumerable<T>的构造函数,以反转元素的顺序创建堆栈的副本。 将显示副本的元素。
创建两倍于堆栈大小的数组,并使用 CopyTo 方法从数组中间开始复制数组元素。 构造 Stack<T> 函数再次用于创建堆栈的副本,其元素顺序相反;因此,三个 null 元素位于末尾。
方法 Contains 用于显示字符串“four”位于堆栈的第一个副本中,之后 Clear 方法清除副本,属性 Count 显示堆栈为空。
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
注解
的 Stack<T> 容量是 可以容纳的元素 Stack<T> 数。 将元素添加到 时 Stack<T>,通过重新分配内部数组,容量会根据需要自动增加。
如果可以估计集合的大小,则指定初始容量就无需在向 Stack<T>中添加元素时执行大量大小调整操作。
可以通过调用 TrimExcess来减小容量。
此构造函数是 O (1) 操作。
适用于
Stack<T>(IEnumerable<T>)
- Source:
- Stack.cs
- Source:
- Stack.cs
- Source:
- Stack.cs
初始化 Stack<T> 类的新实例,该实例包含从指定集合复制的元素并且具有足够的容量来容纳所复制的元素。
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))
参数
- collection
- IEnumerable<T>
从中复制元素的集合。
例外
collection
为 null
。
示例
下面的代码示例演示此构造函数和泛型类的 Stack<T> 多个方法。
该代码示例创建一个具有默认容量的字符串堆栈, Push 并使用 方法将五个字符串推送到堆栈上。 枚举堆栈的元素,这不会更改堆栈的状态。 方法 Pop 用于从堆栈中弹出第一个字符串。 方法 Peek 用于查看堆栈上的下一项,然后使用 Pop 方法将其弹出。
ToArray方法用于创建一个数组并将堆栈元素复制到其中,然后将该数组传递给Stack<T>采用 IEnumerable<T>的构造函数,以反转元素的顺序创建堆栈的副本。 将显示副本的元素。
创建两倍于堆栈大小的数组,并使用 CopyTo 方法从数组中间开始复制数组元素。 构造 Stack<T> 函数再次用于创建堆栈的副本,其元素顺序相反;因此,三个 null 元素位于末尾。
方法 Contains 用于显示字符串“four”位于堆栈的第一个副本中,之后 Clear 方法清除副本,属性 Count 显示堆栈为空。
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
注解
的 Stack<T> 容量是 可以容纳的元素 Stack<T> 数。 将元素添加到 时 Stack<T>,通过重新分配内部数组,容量会根据需要自动增加。
如果可以估计集合的大小,则指定初始容量就无需在向 Stack<T>中添加元素时执行大量大小调整操作。
可以通过调用 TrimExcess来减小容量。
元素按集合的 读取IEnumerator<T>顺序复制到 上Stack<T>。
此构造函数是 O (n
) 操作,其中 n
是 中的 collection
元素数。
另请参阅
适用于
Stack<T>(Int32)
- Source:
- Stack.cs
- Source:
- Stack.cs
- Source:
- Stack.cs
初始化 Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
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)
参数
例外
capacity
小于零。
注解
的 Stack<T> 容量是 可以容纳的元素 Stack<T> 数。 将元素添加到 时 Stack<T>,通过重新分配内部数组,容量会根据需要自动增加。
如果可以估计集合的大小,则指定初始容量就无需在向 Stack<T>中添加元素时执行大量大小调整操作。
可以通过调用 TrimExcess来减小容量。
此构造函数是 O (n
) 操作,其中 n
为 capacity
。