请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
Stack 泛型类

  开启低带宽视图
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
Stack 泛型类

注意:此类在 .NET Framework 2.0 版中是新增的。

表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。

命名空间:System.Collections.Generic
程序集:System(在 system.dll 中)

Visual Basic(声明)
<SerializableAttribute> _
<ComVisibleAttribute(False)> _
Public Class Stack(Of T)
    Implements IEnumerable(Of T), ICollection, _
    IEnumerable
Visual Basic(用法)
Dim instance As Stack(Of T)
C#
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
public class Stack<T> : IEnumerable<T>, ICollection, 
    IEnumerable
C++
[SerializableAttribute] 
[ComVisibleAttribute(false)] 
generic<typename T>
public ref class Stack : IEnumerable<T>, ICollection, 
    IEnumerable
J#
J# 支持使用泛型类型和方法,但不支持进行新的声明。
JScript
JScript 支持泛型类型和方法。

类型参数

T

指定堆栈中的元素的类型。

Stack 作为数组来实现。

Stack 的容量是指 Stack 可以保存的元素数。在此实现中,Stack 的默认初始容量为 10;但此默认值可能在 .NET Framework SDK 的未来版本中更改。向 Stack 添加元素时,将通过重新分配内部数组,根据需要自动增大容量。可通过调用 TrimExcess 来减少容量。

如果 Count 小于堆栈的容量,则 Push 为 O(1) 操作。如果需要增加容量以容纳新元素,则 Push 成为 O(n) 操作,其中 nCountPop 为 O(1) 操作。

Stack 接受 空引用(在 Visual Basic 中为 Nothing) 作为引用类型的有效值并且允许有重复的元素。

下面的代码示例演示了 Stack 泛型类的几种方法。此代码示例创建具有默认容量的字符串堆栈,并使用 Push 方法将五个字符串压入堆栈。枚举堆栈的元素,这不会更改该堆栈的状态。使用 Pop 方法将第一个字符串弹出堆栈。使用 Peek 方法查看此堆栈中的下一个项,然后使用 Pop 方法将其弹出。

使用 ToArray 方法创建数组并将堆栈元素复制到其中,然后将数组传递给具有 IEnumerableStack 构造函数,以元素的反向顺序创建堆栈副本。将显示副本的元素。

创建大小为堆栈大小两倍的数组,并使用 CopyTo 方法从数组的中间开始复制数组元素。再次使用 Stack 构造函数以元素的反向顺序创建堆栈副本;这样,三个空元素就位于堆栈的底部。

使用 Contains 方法显示字符串“four”在第一个堆栈副本中,然后使用 Clear 方法清除该副本,并由 Count 属性显示此堆栈为空。

Visual Basic
Imports System
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
C#
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
 */
System.Object
  System.Collections.Generic.Stack

此类型的公共静态(在 Visual Basic 中为 Shared)成员是线程安全的。但不能保证任何实例成员是线程安全的。

只要不修改该集合,Stack 就可以同时支持多个阅读器。即便如此,从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。若要确保枚举过程中的线程安全,可以在整个枚举过程中锁定集合。若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0

.NET Compact Framework

受以下版本支持:2.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利  |  商标  |  隐私权声明
Page view tracker