C#中泛型容器Stack


    我以前都是學出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("\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 the
27         // constructor that accepts an IEnumerable<T>.
28         Stack<string> stack2 = new Stack<string>(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 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("\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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM