目录
一、Stack<T>集合的概述
表示相同指定类型的实例的可变大小后进先出(LIFO)集合。有序可重复,允许null。
Stack<T>底层是通过数组,来实现先进后出的。当您需要临时存储信息时,Stack和Queue非常有用;也就是说,您可能希望在检索元素的值之后丢弃它。如果需要按照存储在集合中的顺序访问信息,请使用Queue<T>。如果需要以相反的顺序访问信息,则使用System.Collections.GenericStack<T>。
当您需要从多个线程并发访问集合时,请使用System.Collections.Concurrent.ConcurrentStack<T>和System.Collections.Concurrent.ConcurrentQueue<T>类型。
System.Collections.Generic.Stack<T>的一个常用用法,是为了在调用其他过程期间保留变量状态。
在System.Collections.Generic.Stack<T>中可以执行以下三种主要操作:
1、Push()方法,在Stack<T>的顶部插入一个元素。
2、Pop()方法,在Stack<T>的顶部删除一个元素。
3、Peek()方法,从Stack<T>中获取顶部的一个元素,但是不删除。
Stack<T>的容量是指Stack<T>可以容纳的元素数量。当向Stack<T>添加元素时,通过重新分配内部数组,容量会根据需要自动增加。可以通过调用TrimExcess来减少容量。
如果Count小于Stack<T>的容量,则Push()是一个O(1)操作。如果需要增加容量来容纳新元素,Push()就变成一个O(n)操作,其中n是Count。Pop()是一个O(1)操作。
Stack<T>允许null作为引用类型的有效值,并允许重复元素。
二、Stack集合的构造函数
Stack<T>():初始化Stack<T>类的新实例,该实例为空并具有默认初始容量。
Stack<T>(IEnumerable<T>):初始化Stack<T>类的新实例,该实例包含从指定集合复制的元素,并且有足够的容量容纳复制的元素数量。
Stack<T>(Int32):初始化Stack<T>类的新实例,该实例为空,具有指定的初始容量或默认的初始容量,以较大者为准。
三、Stack集合的常用属性
Count:获取Stack<T>中包含的元素数。
四、Stack集合的常用方法
Clear():将集合中的元素清空,全部删除。
Contain(T t):判断集合中是否包含某个元素。
CopyTo(T[] array , Int32 index):从指定的数组索引开始,将Stack<T>元素复制到现有的一维数组中。
EnsureCapacity(Int32 size):确保此Stack的容量至少是指定的容量。如果当前容量小于容量,则依次增加到当前容量的两倍,直到至少达到指定容量。
GetEnumerator():返回Stack<T>的枚举数。
Push(T t):在Stack<T>的顶部插入一个对象。
Pop():返回并移除Stack<T>顶部的对象。
Peek():返回Stack<T>顶部的对象,但不删除它。
ToArray():将Stack<T>集合中的元素复制到一个新数组。
TrimExcess():如果该数目小于当前容量的90%,则将容量设置为Stack<T>中元素的实际数目。
TryPeek(T result):返回一个值,该值指示Stack<T>的顶部是否存在对象,如果存在,则将其复制到result形参中。该对象未从Stack中移除<T>。
TryPop(T result):返回一个值,该值指示Stack<T>的顶部是否存在对象,如果存在,则将其复制到result参数中,并将其从Stack<T>中删除。
五、Stack集合的实例代码
下面的示例代码演示了Stack<T>泛型类的几个方法。示例代码创建一个具有默认容量的字符串Stack,并使用Push()方法将五个字符串压入Stack。枚举堆栈的元素,这不会改变堆栈的状态。Pop()方法用于将第一个字符串从堆栈中弹出。Peek()方法用于查看堆栈上的下一个项目,然后使用Pop()方法将其弹出。
ToArray()方法用于创建一个数组并将Stack元素复制到其中,然后将该数组传递给stack <T>构造函数,该构造函数接受IEnumerable<T>,创建一个元素顺序颠倒的Stack副本。显示副本的元素。
创建一个两倍于Stack大小的数组,并使用CopyTo()方法复制从数组中间开始的数组元素。再次使用Stack<T>构造函数创建一个元素顺序颠倒的Stack副本;因此,三个null元素位于末尾。
Contains()方法用于显示字符串“four”位于Stack的第一个副本中,之后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循环,读取numbers里的数据时,是和进栈的顺序相反,也就是说,先获取numbers里最后一次插入的数据。
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循环,读取stack2里的数据时,是和进栈的顺序相反,也就是说,先获取stack2里最后一次插入的数据。
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];
//array2值: , , ,three,two,one
numbers.CopyTo(array2, numbers.Count);
// Create a second stack, using the constructor that accepts an
// IEnumerable(Of T).
//stack3值: , , ,three,two,one
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
*/
OVER