C#实现自定义列表

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CustomizedList { class MyList<T> where T:IComparable { private T[] array; private int count=0;//表示当前添加的元素的个数 public MyList(int size) { if (size >= 0) { array = new T[size]; } } public MyList() { array = new T[0]; } public int Capacity { get { return array.Length; } } public int Count { get { return count; } } public void Add(T item ) { if (Count == Capacity) //判断元素个数跟列表容量大小是否一样大,如果一样大,说明数组容量不用,需要创建新的数组 { if (Capacity == 0) { array = new T[4];//当数组长度为0的时候,创建一个长度为4的数组 } else { var newArray = new T[Capacity*2];//当长度不为0的时候,我们创建一个长度为原来2倍的数组 Array.Copy(array,newArray,Count);//把旧数组中的元素复制到新的数组中 , 复制前count个 array-->newArray array = newArray; } } array[Count] = item; count++;//元素个数自增 } public T GetItem(int index) { if (index >= 0 && index <= count - 1) { return array[index]; } else { //Console.WriteLine("索引超出了范围"); throw new Exception("索引超出了范围"); } } public T this[int index] { get//当我们通过索引器取值的时候,会调用get块 { return GetItem(index); } set//当我们通过索引器设置值的时候,会调用set块 { if (index >= 0 && index <= count - 1) { array[index] = value; } else { //Console.WriteLine("索引超出了范围"); throw new Exception("索引超出了范围"); } } } public void Insert(int index, T item) { if (index >= 0 && index <= count - 1) { if (Count == Capacity)//容量不够 进行扩容 { var newArray = new T[Capacity*2]; Array.Copy(array,newArray,count); array = newArray;//newArray被GC机制回收 } for (int i = count-1; i >=index; i--) { array[i + 1] = array[i];//把i位置的值放在后面,就是向后移动一个单位 } array[index] = item; count++; } else { throw new Exception("所以超出范围"); } } public void RemoveAt(int index) { if (index >= 0 && index <= count - 1) { for (int i = index + 1; i < count; i++) { array[i - 1] = array[i]; } count--; } else { throw new Exception("索引超出范围"); } } public int IndexOf(T item) { for (int i = 0; i < count; i++) { if (array[i].Equals(item)) { return i; } } return -1; } public int LastIndexOf(T item) { for (int i = Count-1; i >=0; i--) { if (array[i].Equals(item)) { return i; } } return -1; } public void Sort() { for (int j = 0; j < Count-1; j++) { for (int i = 0; i < Count - 1 - j; i++) { if (array[i].CompareTo(array[i + 1]) > 0) { T temp = array[i]; array[i] = array[i + 1]; array[i + 1] = temp; } } } } } //测试 class Program { static void Main(string[] args) { MyList<int> mylist = new MyList<int>(); mylist.Add(234); mylist.Add(14); mylist.Add(24); mylist.Add(24); mylist.Add(90); mylist.Add(274); //mylist[index] mylist.Insert(1,2); //mylist.RemoveAt(-1); mylist[0] = 100;//通过索引器 设置值 for (int i = 0; i < mylist.Count; i++) { //Console.WriteLine(mylist.GetItem(i)); Console.WriteLine(mylist[i]);//通过索引器 取值 } Console.WriteLine(mylist.IndexOf(24)); Console.WriteLine(mylist.LastIndexOf(24)); mylist.Sort(); Console.WriteLine(); for (int i = 0; i < mylist.Count; i++) { //Console.WriteLine(mylist.GetItem(i)); Console.WriteLine(mylist[i]);//通过索引器 取值 } Console.ReadKey(); } } }

__EOF__

本文作者艾孜尔江
本文链接https://www.cnblogs.com/ezhar/p/12913537.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   艾孜尔江  阅读(1860)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示