C# 泛型集合實(shí)例應(yīng)用淺析
C# 泛型集合了解之前我們明白集合是OOP中的一個(gè)重要概念,C#中對(duì)集合的全面支持更是該語(yǔ)言的精華之一。C# 泛型是C# 2.0中的新增元素(C++中稱為模板),主要用于解決一系列類似的問題。這種機(jī)制允許將類名作為參數(shù)傳遞給泛型類型,并生成相應(yīng)的對(duì)象。將泛型(包括類、接口、方法、委托等)看作模板可能更好理解,模板中的變體部分將被作為參數(shù)傳進(jìn)來(lái)的類名稱所代替,從而得到一個(gè)新的類型定義。泛型是一個(gè)比較大的話題,在此不作詳細(xì)解析,有興趣者可以查閱相關(guān)資料。
C# 泛型集合類用起來(lái)十分的方便快捷。在這篇隨筆里面,我將用鏈表來(lái)模擬c#中的 List﹤T﹥ 類的行為,廢話不多說(shuō),下面來(lái)看我的實(shí)現(xiàn)代碼,代碼中已經(jīng)寫了注釋,所以不再對(duì)代碼進(jìn)行額外的說(shuō)明:
- using System.Collections;
- class MyList﹤T﹥
- {
- private MyListNode firstNode;//首節(jié)點(diǎn)
- private int count;//C# 泛型集合-節(jié)點(diǎn)計(jì)數(shù)
- public MyList()
- {
- this.firstNode = null;
- this.count = 0;
- }
- //C# 泛型集合-得到List長(zhǎng)度
- public int GetLength()
- {
- return this.count;
- }
- //增加一個(gè)節(jié)點(diǎn)
- public void AddElement(T data)
- {
- MyListNode first = this.firstNode;
- if(first==null)
- {
- this.firstNode=new MyListNode(data);
- this.count++;
- return;
- }
- while (first.next != null)
- {
- first = first.next;
- }
- first.next = new MyListNode(data);
- this.count++;
- }
- //C# 泛型集合-刪除一個(gè)節(jié)點(diǎn)
- public bool Remove(T data)
- {
- MyListNode first = this.firstNode;
- if (first.data.Equals(data))
- {
- this.firstNode = first.next;
- this.count--;
- return true;
- }
- while (first.next!=null)
- {
- if (first.next.data.Equals(data))
- {
- first.next = first.next.next;
- this.count--;
- return true;
- }
- }
- return false;
- }
- //C# 泛型集合-得到指定索引上的集合元素
- public T GetAtIndex(int index)
- {
- int innercount = 1;
- MyListNode first = this.firstNode;
- if (index ﹥ count)
- {
- throw new Exception("Index out of boundary");
- }
- else
- {
- while (innercount ﹤ index)
- {
- first = first.next;
- innercount++;
- }
- return first.data;
- }
- }
- //在指定的索引上插入新的元素
- public void InsertAtIndex(int index,T data)
- {
- int innercount = 1;
- MyListNode first = this.firstNode;
- if (index ﹥ count)
- {
- throw new Exception("Index out of boundary");
- }
- if (index == 1)
- {
- this.firstNode = new MyListNode(data);
- this.firstNode.next = first;
- }
- else
- {
- while (innercount ﹤ index - 1)
- {
- first = first.next;
- innercount++;
- }
- MyListNode newNode = new MyListNode(data);
- newNode.next = first.next;
- first.next = newNode;
- }
- this.count++;
- }
- //C# 泛型集合-刪除指定索引上的集合元素
- public void RemoveAtIndex(int index)
- {
- int innercount = 1;
- MyListNode first = this.firstNode;
- if (index ﹥ count)
- {
- throw new Exception("Index out of boundary");
- }
- if (index == 1)
- {
- this.firstNode = first.next;
- }
- else
- {
- while (innercount ﹤ index - 1)
- {
- first = first.next;
- innercount++;
- }
- first.next = first.next.next;
- }
- this.count--;
- }
- //C# 泛型集合-刪除集合中的所有元素
- public void RemoveAll()
- {
- this.firstNode = null;
- this.count = 0;
- }
- //為實(shí)現(xiàn)該集合類能用foreach進(jìn)行遍歷
- public IEnumerator GetEnumerator()
- {
- MyListNode first = this.firstNode;
- while (first!= null)
- {
- yield return first.data;
- first = first.next;
- }
- }
- //內(nèi)部節(jié)點(diǎn)類
- private class MyListNode
- {
- public T data { get; set; }//節(jié)點(diǎn)上的元素值
- public MyListNode next { get; set; }//節(jié)點(diǎn)的下一個(gè)節(jié)點(diǎn)
- public MyListNode(T nodeData)
- {
- this.data = nodeData;
- this.next = null;
- }
- }
- }
下面是C# 泛型集合對(duì)這個(gè)模擬類的使用:
- class Program
- {
- static void Main(string[] args)
- {
- MyList﹤string﹥ ml = new MyList﹤string﹥();
- ml.AddElement("xu");
- ml.AddElement("jin");
- ml.AddElement("lin");
- ml.AddElement("love");
- ml.AddElement("jasmine");
- ml.InsertAtIndex(4, "fiercely");
- ml.RemoveAtIndex(2);
- ml.Remove("lin");
- foreach (string s in ml)
- {
- Console.WriteLine(s);
- }
- }
- }
C# 泛型集合實(shí)例應(yīng)用的基本內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# 泛型集合有所幫助。
【編輯推薦】


















