C#中泛型容器Stack<T>
我以前都是学出c,c++,这个学期开始学c#有点不适应,在编程中遇到些问题,所以自己在网上查了些资料,翻了一下书,写一些总结。
关于c#中Stack<T>泛型容器:
《1》stack,是一种数据结构——栈,是一种操作受到限制的线性表,只能在一端插入和删除,FILO(first input Last Output)或LIFO(last input first Output)
我们不用去管它在编译器中是采用什么样的存储结构。

《2》泛型容器:泛型类和泛型方法兼复用性、类型安全和高效率于一身,是与之对应的非泛型的类和方法所不及。泛型广泛用于容器(collections)和对容器操作的方法中。
.NET框架2.0的类库提供一个新的命名空间System.Collections.Generic,其中包含了一些新的基于泛型的容器类。要查找新的泛型容器类(collection classes)的示例代码,
请参见基础类库中的泛型。当然,你也可以创建自己的泛型类和方法,以提供你自己的泛化的方案和设计模式,这是类型安全且高效的。下面的示例代码以一个简单的泛型链表
类作为示范。(多数情况下,推荐使用由.NET框架类库提供的List<T>类,而不是创建自己的表。)类型参数T在多处使用,具体类型通常在这些地方来指明表中元素的类型。
《3》Stack<T>在 vs2013中的定义:
namespace System.Collections.Generic
{
// 摘要:
// 表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。
//
// 类型参数:
// T:
// 指定堆栈中的元素的类型。
[Serializable]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
{
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
public Stack();
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。
//
// 参数:
// collection:
// 从其中复制元素的集合。
//
// 异常:
// System.ArgumentNullException:
// collection 为 null。
public Stack(IEnumerable<T> collection);
//
// 摘要:
// 初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
//
// 参数:
// capacity:
// System.Collections.Generic.Stack<T> 可包含的初始元素数。
//
// 异常:
// System.ArgumentOutOfRangeException:
// capacity 小于零。
public Stack(int capacity); // 摘要:
// 获取 System.Collections.Generic.Stack<T> 中包含的元素数。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中包含的元素数。
public int Count { get; } // 摘要:
// 从 System.Collections.Generic.Stack<T> 中移除所有对象。
public void Clear();
//
// 摘要:
// 确定某元素是否在 System.Collections.Generic.Stack<T> 中。
//
// 参数:
// item:
// 要在 System.Collections.Generic.Stack<T> 中定位的对象。对于引用类型,该值可以为 null。
//
// 返回结果:
// 如果在 System.Collections.Generic.Stack<T> 中找到 item,则为 true;否则为 false。
public bool Contains(T item);
//
// 摘要:
// 从指定数组索引开始将 System.Collections.Generic.Stack<T> 复制到现有一维 System.Array 中。
//
// 参数:
// array:
// 作为从 System.Collections.Generic.Stack<T> 复制的元素的目标位置的一维 System.Array。System.Array
// 必须具有从零开始的索引。
//
// arrayIndex:
// array 中从零开始的索引,将在此处开始复制。
//
// 异常:
// System.ArgumentNullException:
// array 为 null。
//
// System.ArgumentOutOfRangeException:
// arrayIndex 小于零。
//
// System.ArgumentException:
// 源 System.Collections.Generic.Stack<T> 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。
public void CopyTo(T[] array, int arrayIndex);
//
// 摘要:
// 返回 System.Collections.Generic.Stack<T> 的一个枚举数。
//
// 返回结果:
// 用于 System.Collections.Generic.Stack<T> 的 System.Collections.Generic.Stack<T>.Enumerator。
public Stack<T>.Enumerator GetEnumerator();
//
// 摘要:
// 返回位于 System.Collections.Generic.Stack<T> 顶部的对象但不将其移除。
//
// 返回结果:
// 位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Peek();
//
// 摘要:
// 移除并返回位于 System.Collections.Generic.Stack<T> 顶部的对象。
//
// 返回结果:
// 从 System.Collections.Generic.Stack<T> 的顶部移除的对象。
//
// 异常:
// System.InvalidOperationException:
// System.Collections.Generic.Stack<T> 为空。
public T Pop();
//
// 摘要:
// 将对象插入 System.Collections.Generic.Stack<T> 的顶部。
//
// 参数:
// item:
// 要推入到 System.Collections.Generic.Stack<T> 中的对象。对于引用类型,该值可以为 null。
public void Push(T item);
//
// 摘要:
// 将 System.Collections.Generic.Stack<T> 复制到新数组中。
//
// 返回结果:
// 新数组,包含 System.Collections.Generic.Stack<T> 的元素的副本。
public T[] ToArray();
//
// 摘要:
// 如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack<T> 中的实际元素数。
public void TrimExcess(); // 摘要:
// 枚举 System.Collections.Generic.Stack<T> 的元素。
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{ // 摘要:
// 获取枚举数当前位置的元素。
//
// 返回结果:
// System.Collections.Generic.Stack<T> 中位于枚举数当前位置的元素。
//
// 异常:
// System.InvalidOperationException:
// 枚举数定位在该集合的第一个元素之前或最后一个元素之后。
public T Current { get; } // 摘要:
// 释放由 System.Collections.Generic.Stack<T>.Enumerator 使用的所有资源。
public void Dispose();
//
// 摘要:
// Advances the enumerator to the next element of the System.Collections.Generic.Stack<T>.
//
// 返回结果:
// 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
public bool MoveNext();
}
}
}
《4》Stack<T>中方法
| 名称 | 说明 | |
|---|---|---|
![]() |
Clear() |
从 Stack<T> 中移除所有对象。 |
![]() |
Contains(T) |
确定某元素是否在 Stack<T> 中。 |
![]() |
CopyTo(T[], Int32) |
从特定的数组索引处开始,将 Stack<T> 复制到现有一维 Array。 |
![]() |
Equals(Object) |
确定指定的对象是否等于当前对象。(从 Object 继承。) |
![]() |
Finalize() |
在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从 Object 继承。) |
![]() |
GetEnumerator() |
返回的枚举数 Stack<T>。 |
![]() |
GetHashCode() |
作为默认哈希函数。(从 Object 继承。) |
![]() |
GetType() | |
![]() |
MemberwiseClone() | |
![]() |
Peek() |
返回 Stack<T> 顶部的对象而无需移除它。 |
![]() |
Pop() |
移除并返回位于顶部的对象 Stack<T>。 |
![]() |
Push(T) |
将对象插入 Stack<T> 的顶部。 |
![]() |
ToArray() |
将 Stack<T> 复制到新数组。 |
![]() |
ToString() |
返回表示当前对象的字符串。(从 Object 继承。) |
![]() |
TrimExcess() |
如果元素数小于当前容量的 90%,将容量设置为 Stack<T> 中的实际元素数。 |
注:摘自https://msdn.microsoft.com/zh-cn/library/3278tedw.aspx;
《5》代码示例:
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 * ];
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);
}
}
C#中泛型容器Stack<T>的更多相关文章
- C#中泛型容器Stack<T>的用法,以及借此实现”撤销/重做”功能
.Net为我们提供了众多的泛型集合.比如,Stack<T>先进后出,Queue<T>先进先出,List<T>集合元素可排序,支持索引,LinkedList<T ...
- (转)Java中的容器详细总结
Java中的容器详细总结(编辑中) 原文链接:http://anxpp.com/index.php/archives/656/ 注:本文基于 Jdk1.8 编写 通常程序总是根据运行时才知道的某些条件 ...
- 泛型容器单元(Generics.Collections)[3]: TStack<T> 堆栈列表
TQueue 和 TStack, 一个是队列列表, 一个是堆栈列表; 一个是先进先出, 一个是先进后出. TStack 主要有三个方法.一个属性:Push(压栈).Pop(出栈).Peek(查看下一个 ...
- java中的容器问题
小小的总结一下java中的容器问题. 一.三个知识点 1.迭代器 1).java.util.Interator + hasnext(); next(); remove(); 2).java.lang. ...
- C#中泛型和单链表
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类 ...
- 容器vector的使用总结 容器stack(栈)
0.头文件:#include<vector>; using namespace std; 1.定义: vector<type> vec; 2.迭代器 vector<typ ...
- STL中的容器介绍
STL中的容器主要包括序列容器.关联容器.无序关联容器等. 一]序列容器 (1) vector vector 是数组的一种类表示,提供自动管理内存的功能,除非其他类型容器有更好满足程序的要求,否则,我 ...
- C#中泛型的解释(object,list,var,dynamic的区别)
泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能.泛型将类型参数的概念引入 .NET Framework,类型参数使得设计如下类和方法成为可能:这些类和方法将一个或多个类型的 ...
- [C++]STL中的容器
C++11 STL中的容器 一.顺序容器: vector:可变大小数组: deque:双端队列: list:双向链表: forward_list:单向链表: array:固定大小数组: string: ...
随机推荐
- nyoj 127 星际之门(一)
星际之门(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门 ...
- BestCoder Round #67 (div.2) N bulbs(hdu 5600)
N bulbs Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- linux sar 命令详解
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- Java虚拟机学习 - 体系结构 内存模型
一:Java技术体系模块图 二:JVM内存区域模型 1.方法区 也称"永久代” .“非堆”, 它用于存储虚拟机加载的类信息.常量.静态变量.是各个线程共享的内存区域.默认最小值为16MB,最 ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- ALV的报表对用户定义格式的控制(ALV I_SAVE)
很多ALV的报表都需要手动的进行设置格式以使数据看上去更有意义和条理,如果每次进来都重新操作一遍是很烦人的,所以SAP有提供了一个保存格式的功能,保存格式可以是 '缺省设置' 和 '特定用户' 两种 ...
- JHipster的安装
JHipster GitHub地址:https://jhipster.github.io/ 刚开始接触JHipster,理解还不深,此次随笔只是把自己对JHipster的所学记录一下,也算是一种知识的 ...
- 通过克隆MAC地址 破解网通电信封路由
通过克隆MAC地址 破解网通电信封路由 电信封路由方法一:先确定申请上网的电脑单机状态下已经能够上网.就说明该电脑网卡的MAC地址是合法的MAC地址.进入系统的MSDOS方式,发布ipconfig/a ...
- Android学习笔记(2)
今天我继续看Mars老师的Android开发视频教程,看到一个“深入LinearLayout”的时候,发现一个比较好玩的技巧. 控件的layout_weight属性,他是父控件剩余空间的比例. 如果把 ...
- QM04-生产中的QM
集成的计划 生产过程中的生产工序和检验工序逐渐变得更为相互依赖或者说被组合在一起.从而使来自生产和质量检验领域的工作小组共同创建工艺流程. 检验工序 可以把QM检验特性集成到生产计划(PP)模块的工作 ...

