av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

C#泛型集合實例應(yīng)用淺析

C# 泛型集合了解之前我們明白集合是OOP中的一個重要概念,C#中對集合的全面支持更是該語言的精華之一。C# 泛型是C# 2.0中的新增元素(C++中稱為模板),主要用于解決一系列類似的問題。這種機制允許將類名作為參數(shù)傳遞給泛型類型,并生成相應(yīng)的對象。將泛型(包括類、接口、方法、委托等)看作模板可能更好理解,模板中的變體部分將被作為參數(shù)傳進(jìn)來的類名稱所代替,從而得到一個新的類型定義。泛型是一個比較大的話題,在此不作詳細(xì)解析,有興趣者可以查閱相關(guān)資料。

C# 泛型集合類用起來十分的方便快捷。在這篇隨筆里面,我將用鏈表來模擬c#中的 List﹤T﹥ 類的行為,廢話不多說,下面來看我的實現(xiàn)代碼,代碼中已經(jīng)寫了注釋,所以不再對代碼進(jìn)行額外的說明:

 
 
 
  1. using System.Collections;
  2. class MyList﹤T﹥
  3. {
  4. private MyListNode firstNode;//首節(jié)點
  5. private int count;//C# 泛型集合-節(jié)點計數(shù)
  6.  
  7. public MyList()
  8. {
  9. this.firstNode = null;
  10. this.count = 0;
  11. }
  12. //C# 泛型集合-得到List長度
  13. public int GetLength()
  14. {
  15. return this.count;
  16. }
  17. //增加一個節(jié)點
  18. public void AddElement(T data)
  19. {
  20. MyListNode first = this.firstNode;
  21. if(first==null)
  22. {
  23. this.firstNode=new MyListNode(data);
  24. this.count++;
  25. return;
  26. }
  27. while (first.next != null)
  28. {
  29. first = first.next;
  30. }
  31. first.next = new MyListNode(data);
  32. this.count++;
  33. }
  34. //C# 泛型集合-刪除一個節(jié)點
  35. public bool Remove(T data)
  36. {
  37. MyListNode first = this.firstNode;
  38. if (first.data.Equals(data))
  39. {
  40. this.firstNode = first.next;
  41. this.count--;
  42. return true;
  43. }
  44. while (first.next!=null)
  45. {
  46. if (first.next.data.Equals(data))
  47. {
  48. first.next = first.next.next;
  49. this.count--;
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. //C# 泛型集合-得到指定索引上的集合元素
  56. public T GetAtIndex(int index)
  57. {
  58. int innercount = 1;
  59. MyListNode first = this.firstNode;
  60. if (index ﹥ count)
  61. {
  62. throw new Exception("Index out of boundary");
  63. }
  64. else
  65. {
  66. while (innercount ﹤ index)
  67. {
  68. first = first.next;
  69. innercount++;
  70. }
  71. return first.data;
  72. }
  73. }
  74. //在指定的索引上插入新的元素
  75. public void InsertAtIndex(int index,T data)
  76. {
  77. int innercount = 1;
  78. MyListNode first = this.firstNode;
  79. if (index ﹥ count)
  80. {
  81. throw new Exception("Index out of boundary");
  82. }
  83. if (index == 1)
  84. {
  85. this.firstNode = new MyListNode(data);
  86. this.firstNode.next = first;
  87. }
  88. else
  89. {
  90. while (innercount ﹤ index - 1)
  91. {
  92. first = first.next;
  93. innercount++;
  94. }
  95. MyListNode newNode = new MyListNode(data);
  96. newNode.next = first.next;
  97. first.next = newNode;
  98. }
  99. this.count++;
  100. }
  101. //C# 泛型集合-刪除指定索引上的集合元素
  102. public void RemoveAtIndex(int index)
  103. {
  104. int innercount = 1;
  105. MyListNode first = this.firstNode;
  106. if (index ﹥ count)
  107. {
  108. throw new Exception("Index out of boundary");
  109. }
  110. if (index == 1)
  111. {
  112. this.firstNode = first.next;
  113. }
  114. else
  115. {
  116. while (innercount ﹤ index - 1)
  117. {
  118. first = first.next;
  119. innercount++;
  120. }
  121. first.next = first.next.next;
  122. }
  123. this.count--;
  124. }
  125. //C# 泛型集合-刪除集合中的所有元素
  126. public void RemoveAll()
  127. {
  128. this.firstNode = null;
  129. this.count = 0;
  130. }
  131. //為實現(xiàn)該集合類能用foreach進(jìn)行遍歷
  132. public IEnumerator GetEnumerator()
  133. {
  134. MyListNode first = this.firstNode;
  135. while (first!= null)
  136. {
  137. yield return first.data;
  138. first = first.next;
  139. }
  140. }
  141. //內(nèi)部節(jié)點類
  142. private class MyListNode
  143. {
  144. public T data { get; set; }//節(jié)點上的元素值
  145. public MyListNode next { get; set; }//節(jié)點的下一個節(jié)點
  146. public MyListNode(T nodeData)
  147. {
  148. this.data = nodeData;
  149. this.next = null;
  150. }
  151. }
  152. }

下面是C# 泛型集合對這個模擬類的使用:

 
 
 
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. MyList﹤string﹥ ml = new MyList﹤string﹥();
  6. ml.AddElement("xu");
  7. ml.AddElement("jin");
  8. ml.AddElement("lin");
  9. ml.AddElement("love");
  10. ml.AddElement("jasmine");
  11. ml.InsertAtIndex(4, "fiercely");
  12. ml.RemoveAtIndex(2);
  13. ml.Remove("lin");
  14. foreach (string s in ml)
  15. {
  16. Console.WriteLine(s);
  17. }
  18. }
  19. }

C# 泛型集合實例應(yīng)用的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C# 泛型集合有所幫助。


當(dāng)前文章:C#泛型集合實例應(yīng)用淺析
當(dāng)前地址:http://uogjgqi.cn/article/dhhcsdh.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流