• 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中的定义:

         

        
      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<T> : IEnumerable<T>, ICollection, IEnumerable
     13     {
     14         // 摘要: 
     15         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有默认初始容量。
     16         public Stack();
     17         //
     18         // 摘要: 
     19         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例包含从指定的集合中复制的元素并且其容量足以容纳所复制的元素数。
     20         //
     21         // 参数: 
     22         //   collection:
     23         //     从其中复制元素的集合。
     24         //
     25         // 异常: 
     26         //   System.ArgumentNullException:
     27         //     collection 为 null。
     28         public Stack(IEnumerable<T> collection);
     29         //
     30         // 摘要: 
     31         //     初始化 System.Collections.Generic.Stack<T> 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者)。
     32         //
     33         // 参数: 
     34         //   capacity:
     35         //     System.Collections.Generic.Stack<T> 可包含的初始元素数。
     36         //
     37         // 异常: 
     38         //   System.ArgumentOutOfRangeException:
     39         //     capacity 小于零。
     40         public Stack(int capacity);
     41 
     42         // 摘要: 
     43         //     获取 System.Collections.Generic.Stack<T> 中包含的元素数。
     44         //
     45         // 返回结果: 
     46         //     System.Collections.Generic.Stack<T> 中包含的元素数。
     47         public int Count { get; }
     48 
     49         // 摘要: 
     50         //     从 System.Collections.Generic.Stack<T> 中移除所有对象。
     51         public void Clear();
     52         //
     53         // 摘要: 
     54         //     确定某元素是否在 System.Collections.Generic.Stack<T> 中。
     55         //
     56         // 参数: 
     57         //   item:
     58         //     要在 System.Collections.Generic.Stack<T> 中定位的对象。对于引用类型,该值可以为 null。
     59         //
     60         // 返回结果: 
     61         //     如果在 System.Collections.Generic.Stack<T> 中找到 item,则为 true;否则为 false。
     62         public bool Contains(T item);
     63         //
     64         // 摘要: 
     65         //     从指定数组索引开始将 System.Collections.Generic.Stack<T> 复制到现有一维 System.Array 中。
     66         //
     67         // 参数: 
     68         //   array:
     69         //     作为从 System.Collections.Generic.Stack<T> 复制的元素的目标位置的一维 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<T> 中的元素数目大于从 arrayIndex 到目标 array 末尾之间的可用空间。
     84         public void CopyTo(T[] array, int arrayIndex);
     85         //
     86         // 摘要: 
     87         //     返回 System.Collections.Generic.Stack<T> 的一个枚举数。
     88         //
     89         // 返回结果: 
     90         //     用于 System.Collections.Generic.Stack<T> 的 System.Collections.Generic.Stack<T>.Enumerator。
     91         public Stack<T>.Enumerator GetEnumerator();
     92         //
     93         // 摘要: 
     94         //     返回位于 System.Collections.Generic.Stack<T> 顶部的对象但不将其移除。
     95         //
     96         // 返回结果: 
     97         //     位于 System.Collections.Generic.Stack<T> 顶部的对象。
     98         //
     99         // 异常: 
    100         //   System.InvalidOperationException:
    101         //     System.Collections.Generic.Stack<T> 为空。
    102         public T Peek();
    103         //
    104         // 摘要: 
    105         //     移除并返回位于 System.Collections.Generic.Stack<T> 顶部的对象。
    106         //
    107         // 返回结果: 
    108         //     从 System.Collections.Generic.Stack<T> 的顶部移除的对象。
    109         //
    110         // 异常: 
    111         //   System.InvalidOperationException:
    112         //     System.Collections.Generic.Stack<T> 为空。
    113         public T Pop();
    114         //
    115         // 摘要: 
    116         //     将对象插入 System.Collections.Generic.Stack<T> 的顶部。
    117         //
    118         // 参数: 
    119         //   item:
    120         //     要推入到 System.Collections.Generic.Stack<T> 中的对象。对于引用类型,该值可以为 null。
    121         public void Push(T item);
    122         //
    123         // 摘要: 
    124         //     将 System.Collections.Generic.Stack<T> 复制到新数组中。
    125         //
    126         // 返回结果: 
    127         //     新数组,包含 System.Collections.Generic.Stack<T> 的元素的副本。
    128         public T[] ToArray();
    129         //
    130         // 摘要: 
    131         //     如果元素数小于当前容量的 90%,将容量设置为 System.Collections.Generic.Stack<T> 中的实际元素数。
    132         public void TrimExcess();
    133 
    134         // 摘要: 
    135         //     枚举 System.Collections.Generic.Stack<T> 的元素。
    136         [Serializable]
    137         public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
    138         {
    139 
    140             // 摘要: 
    141             //     获取枚举数当前位置的元素。
    142             //
    143             // 返回结果: 
    144             //     System.Collections.Generic.Stack<T> 中位于枚举数当前位置的元素。
    145             //
    146             // 异常: 
    147             //   System.InvalidOperationException:
    148             //     枚举数定位在该集合的第一个元素之前或最后一个元素之后。
    149             public T Current { get; }
    150 
    151             // 摘要: 
    152             //     释放由 System.Collections.Generic.Stack<T>.Enumerator 使用的所有资源。
    153             public void Dispose();
    154             //
    155             // 摘要: 
    156             //     Advances the enumerator to the next element of the System.Collections.Generic.Stack<T>.
    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 Clear()

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

    System_CAPS_pubmethod Contains(T)

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

    System_CAPS_pubmethod CopyTo(T[], Int32)

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

    System_CAPS_pubmethod Equals(Object)

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

    System_CAPS_protmethod Finalize()

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

    System_CAPS_pubmethod GetEnumerator()

    返回的枚举数 Stack<T>。

    System_CAPS_pubmethod GetHashCode()

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

    System_CAPS_pubmethod GetType()

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

    System_CAPS_protmethod MemberwiseClone()

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

    System_CAPS_pubmethod Peek()

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

    System_CAPS_pubmethod Pop()

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

    System_CAPS_pubmethod Push(T)

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

    System_CAPS_pubmethod ToArray()

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

    System_CAPS_pubmethod ToString()

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

    System_CAPS_pubmethod TrimExcess()

    如果元素数小于当前容量的 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<string> numbers = new Stack<string>();
     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("
    Popping '{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 the
    27         // constructor that accepts an IEnumerable<T>.
    28         Stack<string> stack2 = new Stack<string>(numbers.ToArray());
    29 
    30         Console.WriteLine("
    Contents 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 the
    37         // 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 an
    43         // IEnumerable(Of T).
    44         Stack<string> stack3 = new Stack<string>(array2);
    45 
    46         Console.WriteLine("
    Contents of the second copy, with duplicates and nulls:");
    47         foreach( string number in stack3 )
    48         {
    49             Console.WriteLine(number);
    50         }
    51 
    52         Console.WriteLine("
    stack2.Contains("four") = {0}", 
    53             stack2.Contains("four"));
    54 
    55         Console.WriteLine("
    stack2.Clear()");
    56         stack2.Clear();
    57         Console.WriteLine("
    stack2.Count = {0}", stack2.Count);
    58     }
    59 }
    View Code
    弱水三千,先干为敬。
  • 相关阅读:
    使用sequelize-auto生成sequelize的Models
    table中td内容过长自动换行
    编写一个方法,去掉数组的重复元素
    call, apply的用法意义以及区别是什么
    在javascript中使用媒体查询media query
    总结的一些封装好的javascript函数
    只对safari起作用的css hack
    javascrip实现无缝滚动
    Ensures there will be no 'console is undefined' errors
    jQuery.isEmptyObject()函数用于判断指定参数是否是一个空对象。
  • 原文地址:https://www.cnblogs.com/deom/p/5349317.html
Copyright © 2020-2023  润新知