日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
C#泛型集合實(shí)例應(yīng)用淺析

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

C# 泛型集合類(lèi)用起來(lái)十分的方便快捷。在這篇隨筆里面,我將用鏈表來(lái)模擬c#中的 List﹤T﹥ 類(lèi)的行為,廢話不多說(shuō),下面來(lái)看我的實(shí)現(xiàn)代碼,代碼中已經(jīng)寫(xiě)了注釋?zhuān)圆辉賹?duì)代碼進(jìn)行額外的說(shuō)明:

 
 
 
  1. using System.Collections;
  2. class MyList﹤T﹥
  3. {
  4. private MyListNode firstNode;//首節(jié)點(diǎn)
  5. private int count;//C# 泛型集合-節(jié)點(diǎn)計(jì)數(shù)
  6.  
  7. public MyList()
  8. {
  9. this.firstNode = null;
  10. this.count = 0;
  11. }
  12. //C# 泛型集合-得到List長(zhǎng)度
  13. public int GetLength()
  14. {
  15. return this.count;
  16. }
  17. //增加一個(gè)節(jié)點(diǎn)
  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# 泛型集合-刪除一個(gè)節(jié)點(diǎn)
  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. //為實(shí)現(xiàn)該集合類(lèi)能用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é)點(diǎn)類(lèi)
  142. private class MyListNode
  143. {
  144. public T data { get; set; }//節(jié)點(diǎn)上的元素值
  145. public MyListNode next { get; set; }//節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
  146. public MyListNode(T nodeData)
  147. {
  148. this.data = nodeData;
  149. this.next = null;
  150. }
  151. }
  152. }

下面是C# 泛型集合對(duì)這個(gè)模擬類(lèi)的使用:

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


新聞標(biāo)題:C#泛型集合實(shí)例應(yīng)用淺析
網(wǎng)址分享:http://www.5511xx.com/article/dhhcsdh.html