小白教程
所有教程
关于
Search
172.70.100.63
172.70.100.63
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Csharp stack”的源代码
本页内容
上一节:
Csharp_sortedlist
下一节:
Csharp_queue
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:堆栈(Stack)}}[[Category:csharp collection|4]] = C Sharp 堆栈(Stack) = [[csharp collection|C Sharp 集合]] 堆栈(Stack)代表了一个'''后进先出'''的对象集合。当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为'''推入'''元素,当您从列表中移除一项时,称为'''弹出'''元素。 == Stack 类的方法和属性 == 下表列出了 '''Stack''' 类的一些常用的 '''属性''': {| class="table table-striped table-hover" ! 属性 ! 描述 |- | Count | 获取 Stack 中包含的元素个数。 |} 下表列出了 '''Stack''' 类的一些常用的 '''方法''': {| class="table table-striped table-hover" ! 序号 ! 方法名 & 描述 |- | 1 | '''public virtual void Clear();''' 从 Stack 中移除所有的元素。 |- | 2 | '''public virtual bool Contains( object obj );''' 判断某个元素是否在 Stack 中。 |- | 3 | '''public virtual object Peek();'''返回在 Stack 的顶部的对象,但不移除它。 |- | 4 | '''public virtual object Pop();'''移除并返回在 Stack 的顶部的对象。 |- | 5 | '''public virtual void Push( object obj );'''向 Stack 的顶部添加一个对象。 |- | 6 | '''public virtual object[] ToArray();'''复制 Stack 到一个新的数组中。 |} 下面的实例演示了堆栈(Stack)的使用: <sample title="" desc="" lang="csharp" hererun="1"> using System; using System.Collections; namespace CollectionsApplication { class Program { static void Main(string[] args) { Stack st = new Stack(); st.Push('A'); st.Push('M'); st.Push('G'); st.Push('W'); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); st.Push('V'); st.Push('H'); Console.WriteLine("The next poppable value in stack: {0}", st.Peek()); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } Console.WriteLine(); Console.WriteLine("Removing values "); st.Pop(); st.Pop(); st.Pop(); Console.WriteLine("Current stack: "); foreach (char c in st) { Console.Write(c + " "); } } } } </sample> 当上面的代码被编译和执行时,它会产生下列结果: <sample title="" desc="" lang="csharp" hererun="1"> Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A </sample> [[csharp collection|C Sharp 集合]]
返回至“
Csharp stack
”。
上一节:
Csharp_sortedlist
下一节:
Csharp_queue