C# 实现动态数组

编程入门 行业动态 更新时间:2024-10-28 00:14:23

C# 实现动态<a href=https://www.elefans.com/category/jswz/34/1771288.html style=数组"/>

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# 实现动态数组

本文发布于:2023-11-14 14:30:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!