博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中泛型容器Stack<T>
阅读量:4655 次
发布时间:2019-06-09

本文共 7676 字,大约阅读时间需要 25 分钟。

    我以前都是学出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中的定义:

     

    
1 namespace System.Collections.Generic  2 {  3     // 摘要:   4     //     表示同一任意类型的实例的大小可变的后进先出 (LIFO) 集合。  5     //  6     // 类型参数:   7     //   T:  8     //     指定堆栈中的元素的类型。  9     [Serializable] 10     [ComVisible(false)] 11     [DebuggerDisplay("Count = {Count}")] 12     public class Stack
: IEnumerable
, ICollection, IEnumerable 13 { 14 // 摘要: 15 // 初始化 System.Collections.Generic.Stack
类的新实例,该实例为空并且具有默认初始容量。 16 public Stack(); 17 // 18 // 摘要: 19 // 初始化 System.Collections.Generic.Stack
类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。 20 // 21 // 参数: 22 // collection: 23 // 从其中复制元素的集合。 24 // 25 // 异常: 26 // System.ArgumentNullException: 27 // collection 为 null。 28 public Stack(IEnumerable
collection); 29 // 30 // 摘要: 31 // 初始化 System.Collections.Generic.Stack
类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。 32 // 33 // 参数: 34 // capacity: 35 // System.Collections.Generic.Stack
可包含的初始元素数。 36 // 37 // 异常: 38 // System.ArgumentOutOfRangeException: 39 // capacity 小于零。 40 public Stack(int capacity); 41 42 // 摘要: 43 // 获取 System.Collections.Generic.Stack
中包含的元素数。 44 // 45 // 返回结果: 46 // System.Collections.Generic.Stack
中包含的元素数。 47 public int Count { get; } 48 49 // 摘要: 50 // 从 System.Collections.Generic.Stack
中移除所有对象。 51 public void Clear(); 52 // 53 // 摘要: 54 // 确定某元素是否在 System.Collections.Generic.Stack
中。 55 // 56 // 参数: 57 // item: 58 // 要在 System.Collections.Generic.Stack
中定位的对象。对于引用类型,该值可以为 null。 59 // 60 // 返回结果: 61 // 如果在 System.Collections.Generic.Stack
中找到 item,则为 true;否则为 false。 62 public bool Contains(T item); 63 // 64 // 摘要: 65 // 从指定数组索引开始将 System.Collections.Generic.Stack
复制到现有一维 System.Array 中。 66 // 67 // 参数: 68 // array: 69 // 作为从 System.Collections.Generic.Stack
复制的元素的目标位置的一维 System.Array。System.Array 70 // 必须具有从零开始的索引。 71 // 72 // arrayIndex: 73 // array 中从零开始的索引,将在此处开始复制。 74 // 75 // 异常: 76 // System.ArgumentNullException: 77 // array 为 null。 78 // 79 // System.ArgumentOutOfRangeException: 80 // arrayIndex 小于零。 81 // 82 // System.ArgumentException: 83 // 源 System.Collections.Generic.Stack
中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。 84 public void CopyTo(T[] array, int arrayIndex); 85 // 86 // 摘要: 87 // 返回 System.Collections.Generic.Stack
的一个枚举数。 88 // 89 // 返回结果: 90 // 用于 System.Collections.Generic.Stack
的 System.Collections.Generic.Stack
.Enumerator。 91 public Stack
.Enumerator GetEnumerator(); 92 // 93 // 摘要: 94 // 返回位于 System.Collections.Generic.Stack
顶部的对象但不将其移除。 95 // 96 // 返回结果: 97 // 位于 System.Collections.Generic.Stack
顶部的对象。 98 // 99 // 异常: 100 // System.InvalidOperationException:101 // System.Collections.Generic.Stack
为空。102 public T Peek();103 //104 // 摘要: 105 // 移除并返回位于 System.Collections.Generic.Stack
顶部的对象。106 //107 // 返回结果: 108 // 从 System.Collections.Generic.Stack
的顶部移除的对象。109 //110 // 异常: 111 // System.InvalidOperationException:112 // System.Collections.Generic.Stack
为空。113 public T Pop();114 //115 // 摘要: 116 // 将对象插入 System.Collections.Generic.Stack
的顶部。117 //118 // 参数: 119 // item:120 // 要推入到 System.Collections.Generic.Stack
中的对象。对于引用类型,该值可以为 null。121 public void Push(T item);122 //123 // 摘要: 124 // 将 System.Collections.Generic.Stack
复制到新数组中。125 //126 // 返回结果: 127 // 新数组,包含 System.Collections.Generic.Stack
的元素的副本。128 public T[] ToArray();129 //130 // 摘要: 131 // 如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack
中的实际元素数。132 public void TrimExcess();133 134 // 摘要: 135 // 枚举 System.Collections.Generic.Stack
的元素。136 [Serializable]137 public struct Enumerator : IEnumerator
, IDisposable, IEnumerator138 {139 140 // 摘要: 141 // 获取枚举数当前位置的元素。142 //143 // 返回结果: 144 // System.Collections.Generic.Stack
中位于枚举数当前位置的元素。145 //146 // 异常: 147 // System.InvalidOperationException:148 // 枚举数定位在该集合的第一个元素之前或最后一个元素之后。149 public T Current { get; }150 151 // 摘要: 152 // 释放由 System.Collections.Generic.Stack
.Enumerator 使用的所有资源。153 public void Dispose();154 //155 // 摘要: 156 // Advances the enumerator to the next element of the System.Collections.Generic.Stack
.157 //158 // 返回结果: 159 // 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。160 //161 // 异常: 162 // System.InvalidOperationException:163 // 在创建了枚举数后集合被修改了。164 public bool MoveNext();165 }166 }167 }
View Code

    《4》Stack<T>中方法

 
 
  名称 说明
System_CAPS_pubmethod

从 Stack<T> 中移除所有对象。

System_CAPS_pubmethod

确定某元素是否在 Stack<T> 中。

System_CAPS_pubmethod

从特定的数组索引处开始,将 Stack<T> 复制到现有一维 。

System_CAPS_pubmethod

确定指定的对象是否等于当前对象。(从  继承。)

System_CAPS_protmethod

在垃圾回收将某一对象回收前允许该对象尝试释放资源并执行其他清理操作。(从  继承。)

System_CAPS_pubmethod

返回的枚举数 Stack<T>。

System_CAPS_pubmethod

作为默认哈希函数。(从  继承。)

System_CAPS_pubmethod

获取当前实例的 。(从  继承。)

System_CAPS_protmethod

创建当前  的浅表副本。(从  继承。)

System_CAPS_pubmethod

返回 Stack<T> 顶部的对象而无需移除它。

System_CAPS_pubmethod

移除并返回位于顶部的对象 Stack<T>。

System_CAPS_pubmethod

将对象插入 Stack<T> 的顶部。

System_CAPS_pubmethod

将 Stack<T> 复制到新数组。

System_CAPS_pubmethod

返回表示当前对象的字符串。(从  继承。)

System_CAPS_pubmethod

如果元素数小于当前容量的 90%,将容量设置为 Stack<T> 中的实际元素数。

    注:摘自https://msdn.microsoft.com/zh-cn/library/3278tedw.aspx;

  《5》代码示例:

    

  
1 using System; 2 using System.Collections.Generic; 3  4 class Example 5 { 6     public static void Main() 7     { 8         Stack
numbers = new Stack
(); 9 numbers.Push("one");10 numbers.Push("two");11 numbers.Push("three");12 numbers.Push("four");13 numbers.Push("five");14 15 // A stack can be enumerated without disturbing its contents.16 foreach( string number in numbers )17 {18 Console.WriteLine(number);19 }20 21 Console.WriteLine("\nPopping '{0}'", numbers.Pop());22 Console.WriteLine("Peek at next item to destack: {0}", 23 numbers.Peek());24 Console.WriteLine("Popping '{0}'", numbers.Pop());25 26 // Create a copy of the stack, using the ToArray method and the27 // constructor that accepts an IEnumerable
.28 Stack
stack2 = new Stack
(numbers.ToArray());29 30 Console.WriteLine("\nContents of the first copy:");31 foreach( string number in stack2 )32 {33 Console.WriteLine(number);34 }35 36 // Create an array twice the size of the stack and copy the37 // elements of the stack, starting at the middle of the 38 // array. 39 string[] array2 = new string[numbers.Count * 2];40 numbers.CopyTo(array2, numbers.Count);41 42 // Create a second stack, using the constructor that accepts an43 // IEnumerable(Of T).44 Stack
stack3 = new Stack
(array2);45 46 Console.WriteLine("\nContents of the second copy, with duplicates and nulls:");47 foreach( string number in stack3 )48 {49 Console.WriteLine(number);50 }51 52 Console.WriteLine("\nstack2.Contains(\"four\") = {0}", 53 stack2.Contains("four"));54 55 Console.WriteLine("\nstack2.Clear()");56 stack2.Clear();57 Console.WriteLine("\nstack2.Count = {0}", stack2.Count);58 }59 }
View Code

 

转载于:https://www.cnblogs.com/deom/p/5349317.html

你可能感兴趣的文章
JS中for循环输出三角形
查看>>
字节对齐2
查看>>
与Win8之磁盘活动时间100%斗争心得
查看>>
Matrix: android 中的Matrix (android.graphics.Matrix) (转)
查看>>
Android中处理崩溃异常
查看>>
Day7—socket进阶
查看>>
只读数据文件损坏恢复
查看>>
转过来的,可以看下
查看>>
windows搭建SVN服务MD版
查看>>
Java私塾的一些基础练习题(一)
查看>>
Shell 07 项目案例
查看>>
Dapper基础用法
查看>>
一步步学习SPD2010--第九章节--使用可重用工作流和工作流表单(1)--创建和使用可重用工作流...
查看>>
Eclipse配置默认的编码集为utf-8
查看>>
【精解】EOS标准货币体系与源码实现分析
查看>>
HashMap
查看>>
Android广播机制概述
查看>>
[javascript]9宫格拖拽拼图游戏 puzzle
查看>>
Entity Framework底层操作封装(3)
查看>>
InputStream 转换 InputStreamReader再转换BufferedReader
查看>>