C#中泛型容器Stack<T>
2016-04-03 10:35
337 查看
我以前都是学出c,c++,这个学期开始学c#有点不适应,在编程中遇到些问题,所以自己在网上查了些资料,翻了一下书,写一些总结。
关于c#中Stack<T>泛型容器:
《1》stack,是一种数据结构——栈,是一种操作受到限制的线性表,只能在一端插入和删除,FILO(first input Last Output)或LIFO(last input first Output)
我们不用去管它在编译器中是采用什么样的存储结构。
View Code
关于c#中Stack<T>泛型容器:
《1》stack,是一种数据结构——栈,是一种操作受到限制的线性表,只能在一端插入和删除,FILO(first input Last Output)或LIFO(last input first Output)
我们不用去管它在编译器中是采用什么样的存储结构。
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); } }
View Code
相关文章推荐
- C#套接字和windowsAPI套接字
- C#套接字和windowsAPI套接字
- C# LDAP 管理(创建新用户)
- C# AD(Active Directory)域信息同步,组织单位、用户等信息查询
- C#-变量类型(值类型、引用类型)
- C# 服务器、客户端学习(四)
- C# 服务器、客户端学习(三)
- C# 服务器、客户端学习(二)
- C# 客户端服务器学习(一)
- BC#78Div.2 1001
- C#制作视频播放器-player播放器组件
- C# Chart详细解析
- [C#]使textbox光标在最左边
- [C#]textbox获取光标
- C# Chart控件,chart、Series、ChartArea曲线图绘制的重要属性
- C#制作一个消息拦截器(intercept)1
- 改善C#程序的50种方法
- C# textbox快捷键添加横杠
- c# 技巧之 泛型方法
- C# 调用IE打开网页链接 or 直接使用默认浏览器打开网页链接