定義一個數據自己的列表MyList ?使用上述描述列表的方式(數組) ?列表內也要定義屬于自己的方法 ?例如 Sort排序 Add添加 等等....
思路
┌─────────────────────────────────────────────────────────────────┐
│ 開始:實現 MyList<T> 泛型類 │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 1. 定義核心成員(存儲與計數) │
│ ┌───────────────┐ ┌───────────────┐ │
│ │ private T[] data │ │ private int count │ │
│ │ (內部存儲數組) │ │ (實際元素數量) │ │
│ └───────────────┘ └───────────────┘ │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 2. 實現基礎屬性(對外暴露信息) │
│ ┌────────────────────────┐ ┌────────────────────────┐ │
│ │ public int Capacity │ │ public int Count │ │
│ │ (獲取數組容量) │ │ (獲取元素總數) │ │
│ │ get { return data.Length; } get { return count; } │ │
│ └────────────────────────┘ └────────────────────────┘ │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 3. 實現核心功能方法 │
├───────────────┬───────────────┬───────────────┬───────────────┐ │
│ Add(T item) │ Insert(...) │ RemoveAt(...) │ IndexOf(...) │ │
│ (添加元素) │ (插入元素) │ (刪除元素) │ (查找索引) │ │
├───────────────┼───────────────┼───────────────┼───────────────┤ │
│ LastIndexOf(...) │ Sort() │ 索引器 [...] │ KR()擴容 │ │
│ (反向查索引) │ (排序) │ (元素訪問) │ (內部用) │ │
└───────────────┴───────────────┴───────────────┴───────────────┘ │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 4. 支持高級特性(遍歷與初始化) │
│ ┌─────────────────────────────────┐ ┌─────────────────────┐ │
│ │ 實現 IEnumerable<T> 接口 │ │ 添加構造函數 │ │
│ │ - GetEnumerator() │ │ - 接收 IEnumerable │ │
│ │ - 用 yield return 遍歷元素 │ │ - 批量添加元素 │ │
│ └─────────────────────────────────┘ └─────────────────────┘ │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 5. 測試驗證 │
│ - 集合初始化器:new MyList<int> {1,2,3} │
│ - foreach 遍歷元素 │
│ - 增/刪/查/改/排序功能驗證 │
└───────────────────────────────────┬─────────────────────────────┘↓
┌─────────────────────────────────────────────────────────────────┐
│ 結束:功能完成 │
└─────────────────────────────────────────────────────────────────┘
首先定義一個 MyList<> 泛型類
internal class MyList<T> : IEnumerable<T>{private T[] data = new T[0];private int count = 0;//數據個數public int Capacity //獲取或設置列表的容量{get { return data.Length; }}public void Add(T item){KR();data[count] = item;count++;}//添加數據public int Count //Count數據個數,不能set設置count值,放止外界修改{get{return count;}}// 集合初始化器構造函數public MyList(IEnumerable<T> collection)//collection代表集合 {}{foreach (var item in collection){Add(item);}}// 無參構造函數public MyList() { }#region 插入數據的方法 Insertpublic void Insert(int index, T item){if (index < 0 || index > count - 1){throw new IndexOutOfRangeException("索引參數超出范圍了");}KR();for (int i = count - 1; i > index - 1; i--){data[i + 1] = data[i];}data[index] = item;count++;}#endregion#region 擴容private void KR(){if (data.Length == 0){data = new T[4];}//添加元素前,判斷數組是否滿if (data.Length == count){T[] temp = new T[count * 2];for (int i = 0; i < data.Length; i++){temp[i] = data[i];}data = temp;}}#endregion#region 索引器 [index]//dx通過索引器訪問方法public T this[int index]{get{if (index < 0 || index > count - 1){throw new IndexOutOfRangeException("索引參數超出范圍了");}return data[index];}set{data[index] = value;}}#endregion #region 整數類型排序 Sortpublic void Sort(){bool isChange ;//標記排序是否完成int num = 0; //記錄交換次數for (int i = 0; i < count - 1; i++){isChange = false; // 每次外層循環開始時重置為falsefor (int j = 0; j < count - 1-i; j++){if ((dynamic)data[j] > (dynamic)data[j + 1]){T temp = data[j];data[j] = data[j + 1];data[j + 1] = temp;isChange = true;num++;}}if (isChange == false){ break; }}Console.WriteLine($"交換了:{num}次");}#endregion#region 索引刪除數據 RemoveAtpublic void RemoveAt(int index){if (index < 0 || index > count - 1){throw new IndexOutOfRangeException("索引參數超出范圍了");}for (int i = index; i < count-1; i++){data[i] = data[i + 1]; }count--;}#endregion#region 取得第一個對應元素所在列表中的索引位置 IndexOfpublic int IndexOf(T item){int index=-1;//定義一個索引值for (int i = 0; i < count; i++){if (item.Equals(data[i])){index=i;break;}}if (index == -1){Console.WriteLine("沒有要查找的數據");}return index;}#endregion#region 取得最后一個對應元素所在列表中的索引位置 LastIndexOfpublic int LastIndexOf(T item){int index = -1;//定義一個索引值for (int i = count-1; i >= 0; i--){if (item.Equals(data[i])){index = i; break;}}if (index == -1){Console.WriteLine("沒有要查找的數據");}return index;}#endregion#region 實現IEnumerable<T>接口,支持foreach遍歷public IEnumerator<T> GetEnumerator(){for (int i = 0; i < count; i++){yield return data[i];}}IEnumerator IEnumerable.GetEnumerator(){return GetEnumerator();}#endregion}
主函數測試
static void Main(string[] args){MyList <int> list= new MyList<int>() { 1,5,6,88};list.Add(1);list.Add(5);list.Add(3);list.Add(4);list.Add(9);list.Add(6);list.Insert(0, 9);//在索引為0的位置添加一個數據Console.WriteLine("取得第一個對應元素所在列表中的索引位置"+list.IndexOf(9));Console.WriteLine("取得最后一個對應元素所在列表中的索引位置" + list.LastIndexOf(9));//刪除索引位置在1的數據list.RemoveAt(1);//從小到大排序list.Sort ();Console.Write("列表數據包含:");foreach (int item in list){Console.Write(item + " ");}Console.WriteLine("元素個數:" + list.Count);}
注:IEnumerable<T>
IEnumerable<T>
?是 C# 中定義在?System.Collections.Generic
?命名空間下的泛型接口,用于表示一個可枚舉的集合。它是 .NET 集合框架中非常基礎且重要的接口,所有支持?foreach
?循環遍歷?的集合都實現了這個接口(或其非泛型版本?IEnumerable
)。
核心作用
- 提供?統一的遍歷方式:無論集合內部結構如何(數組、鏈表、哈希表等),只要實現了?
IEnumerable<T>
,就能通過?foreach
?循環遍歷元素。 - 支持?延遲執行:配合 LINQ 時,可以實現查詢的延遲執行(只在真正需要結果時才計算),提高性能。
接口定義
IEnumerable<T>
?接口非常簡單,只包含一個方法:
public interface IEnumerable<out T> : IEnumerable
{// 返回一個用于遍歷集合的枚舉器IEnumerator<T> GetEnumerator();
}
其中:
IEnumerator<T>
?是另一個接口,負責實際遍歷邏輯,包含?MoveNext()
(移動到下一個元素)、Current
(獲取當前元素)等成員。foreach
?循環的本質就是調用?GetEnumerator()
?獲取枚舉器,然后通過枚舉器遍歷元素。
例如,在 本代碼中添加以下實現:
using System.Collections;
using System.Collections.Generic;public class MyList<T> : IEnumerable<T>
{// 其他現有代碼...// 實現 IEnumerable<T> 接口public IEnumerator<T> GetEnumerator(){for (int i = 0; i < count; i++){yield return data[i]; // 逐個返回元素,支持 foreach 遍歷}}// 非泛型版本的實現(接口繼承要求)IEnumerator IEnumerable.GetEnumerator(){return GetEnumerator();}
}