数组"/>
C# 实现动态数组
题目
实现动态数组(非固定大小的数组),不能使用 STL 如Vector 或C# 容器库来实现。
最近我在做这道题,结果做得很不好。刚开始有两种思路:
(1)以链表的形式来实现,想利用一个元素和一个指针来操作一组数据。
但是以链表的方式去实现,已经不能算是动态数组了。
(2)使用泛型来实现。
当时有想到过利用泛型和枚举器来操作数组,但是已经忘记怎么写了。
分析题目:
- 非固定大小数组,即对数组进行增删时,数组的长度大小跟进变化。
- 数组存储的元素类型未知,可以使用泛型来实现。
- 参考 List《T》容器,来实现一个动态数组。
(1)定义泛型类
该知识点的文章:
(八)CSharp-泛型类和参数约束(1)_csharp 泛型-CSDN博客
public class DynList<T>{private T[] arry;private int Capacity { get; set; }//容器大小public int Count { get { return arry == null ? 0:arry.Length; } }public DynList(int capacity){Capacity = capacity;arry = new T[capacity];}// 参考 List 容器,创建后,arry 默认为 new arr[1];public DynList(){Capacity = 0;arry = new T[1];}}
(2)定义枚举器
在外部用来遍历数组元素的,即foreach(var item in list)。
该知识点的文章:
(九)枚举器和迭代器(2)_enumerator 迭代器-CSDN博客
//枚举器public IEnumerable<T> Foreach(){foreach(var item in arry){yield return item;}}
后面就编写实现对数组操作的函数,如Add,Remove等。
(3)完整代码
public class DynList<T>{private T[] arry;private int Capacity { get; set; }public int Count { get { return arry == null ? 0:arry.Length; } }public DynList(int capacity){Capacity = capacity;arry = new T[capacity];}public DynList(){Capacity = 0;arry = new T[1];}//添加元素public void Add(T item){if(Capacity == 0){arry = new T[1];arry[0] = item;Capacity = 1;}else{var arryTemp = new T[Capacity + 1];arry.CopyTo(arryTemp, 0);arryTemp[Capacity] = item;Capacity++;arry = arryTemp;}}//在 index 位置插入 item 元素public void Insert(int index, T item){if (Capacity == 0){if(index == 0){arry = new T[1];arry[0] = item;Capacity = 1;}}else{if (index >= arry.Length)return;var arryTemp = new T[Capacity + 1];arryTemp[index] = item;T temp = item;bool insertEndElement = false;for (int i = 0; i < arry.Length; i++){//保留index前的元素if (index > i)arryTemp[i] = arry[i];else{//index后的元素向后移arryTemp[i] = temp;temp = arry[i];if (i == arry.Length - 1)insertEndElement = true;}}if (insertEndElement)arryTemp[arryTemp.Length - 1] = temp;Capacity ++;arry = arryTemp;}}//移除 item 元素public bool Remove(T item){if (Capacity <= 0)return false;var arryTemp = new T[Capacity - 1];int removeIndex = -1;for(int i = 0; i < arryTemp.Length;i++){if(arry[i].Equals(item)){removeIndex = i;}else{arryTemp[i] = arry[i];//保留index前的元素}}for (int i = removeIndex; i < arryTemp.Length; i++){//从 index 位置开始,把原数组的 index +1 元素填补。arryTemp[removeIndex] = arry[i + 1];}if (removeIndex != -1){Capacity--;arry = arryTemp;}return true;}//移除 index 位置的元素public void RemoveAt(int index){if (Capacity <= 0 || Capacity < index + 1)return;if (Capacity == 1){if(index == 0){arry = new T[1];Capacity = 1;}return;}var arryTemp = new T[Capacity - 1];int removeIndex = -1;for (int i = 0; i < arryTemp.Length;i++){if(i == index){removeIndex = i;break;}else{arryTemp[i] = arry[i];}}if (index == Capacity - 1)removeIndex = index;for (int i = removeIndex; i < arryTemp.Length; i++){arryTemp[removeIndex] = arry[i + 1];}if (removeIndex != -1){Capacity--;arry = arryTemp;}}//枚举器public IEnumerable<T> Foreach(){foreach(var item in arry){yield return item;}}}class Program{static void Main(string[] args){DynList<int> arry = new DynList<int>();arry.Add(0);arry.Add(1);arry.Add(2);arry.Insert(0,6);arry.Remove(1);arry.RemoveAt(4);foreach (var item in arry.Foreach()){ }}}
更多推荐
C# 实现动态数组
发布评论