【C#】实现静态数组、动态数组、泛型动态数组


任务

静态数组的实现任务

实现目标:(1)实现一个静态数组

     (2)数组可以增,删,改,查;

     (3)可以输出数组信息;

实现过程:

(1)创建一个新的类,包含字段元素存储,元素个数;

(2)创建构造函数,分别是一个参数的构造函数与无参数的构造函数;

(3)实现增功能,允许按照下标添加;

(4)实现查询功能,按照下标查询,按照给出元素查询;

(5)实现删除功能,按照下标删除某个元素,按照给出元素删除,删除所有元素;

(7)实现修改功能,按照下标修改,按照给出元素修改;

(8)实现输出函数;

动态数组的实现任务

实现目标:(1)对静态数组容量进行修改,使得数组可以通过实际存储的数据多少进行改变。

实现过程:(1)在原有的静态数组上新增一个自动扩容缩容的方法

实现泛型动态数组

实现目标:(1)数组不再是只是整形的动态数组,可以传入泛型

实现过程:(1)在原有的整形动态数组上添加泛型,修改代码

重点:

(1)无参构造函数的实现方法

1 public Array1() : this(10) { }
2         //与上面无参数函数等价
3         //public Array1()
4         //{
5         //    data = new int[10];
6         //    N = 0;
7         //}

(2)ToString()方法的重写

 1 /// 
 2         /// 重写ToString方法否则不能正常输出
 3         /// 
 4         /// 
 5         public override string ToString()
 6         {
 7             //实例化
 8             StringBuilder res = new StringBuilder();
 9             //重写格式1:输出数组元素个数以及长度
10             res.Append(string.Format("Array1:   count={0}    capacity={1}\n",N,data.Length));
11             res.Append('[');
12             //输出元素
13             for (int i = 0; i < N; i++)
14             {
15                 res.Append(data[i]);
16                 if (i!=N-1)
17                 {
18                     res.Append(',');
19                 }
20             }
21             res.Append(']'+"\n");
22             //返回
23             return res.ToString();
24         }
25     }

(3)动态数组扩容缩容实现方法1

 1         /// 
 2         /// 扩容缩容数组
 3         /// 
 4         /// 
 5         private void ResetCapacity(int newcapacity)
 6         {
 7             int[] newdata = new int[newcapacity];
 8             for (int i = 0; i < N; i++)
 9             {
10                 newdata[i] = data[i]; 
11             }
12             data = newdata;
13         }

 (4)泛型查找元素的比较

 1 /// 
 2         /// 查询特定元素
 3         /// 
 4         /// 
 5         /// 
 6         public int Find(E d)
 7         {
 8             for (int i = 0; i < N; i++)
 9             {
10                 if (data[i].Equals(d))
11                 {
12                     return i;
13                 }
14             }
15             return -1;
16         }

动态数组完整代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace DataStructrue
  8 {
  9     class Array1
 10     {
 11         /// 
 12         /// 数组的元素存储
 13         /// 
 14         private int[] data;
 15         /// 
 16         /// 数组元素数量
 17         /// 
 18         private int N;
 19 
 20         public Array1(int capacity)
 21         {
 22             data = new int[capacity];
 23             N = 0;
 24         }
 25         public Array1() : this(10) { }
 26         //与上面无参数函数等价
 27         //public Array1()
 28         //{
 29         //    data = new int[10];
 30         //    N = 0;
 31         //}
 32         
 33         /// 
 34         /// 在某位置添加新元素
 35         /// 
 36         /// 
 37         /// 
 38         public void Add(int index,int d)
 39         {
 40             if (index>N || index <0)
 41             {
 42                 throw new ArgumentException("数组索引越界");
 43             }
 44             else if (N==data.Length)
 45             {
 46                 ResetCapacity(2*N);
 47             }
 48             for (int i = N; i >= index; i--)
 49             {
 50                 if (N != data.Length-1)
 51                 {
 52                     data[i + 1] = data[i];
 53                 }
 54             }
 55             data[index] = d;
 56             N++;
 57         }
 58         public void Addlast(int d)
 59         {
 60             Add(N, d);
 61         }
 62         /// 
 63         /// 删除下标元素
 64         /// 
 65         /// 
 66         public void RemoveAt(int index)
 67         {
 68             if (index > N || index < 0)
 69             {
 70                 throw new ArgumentException("数组索引越界");
 71             }
 72             for (int i = index; i < N; i++)
 73             {
 74                 data[i] = data[i + 1];
 75             }
 76             N--;
 77             if (N==(data.Length/4))
 78             {
 79                 ResetCapacity(data.Length / 2);
 80             }
 81             data[N] = default;
 82         }
 83         /// 
 84         /// 删除特定元素
 85         /// 
 86         /// 
 87         public void Remove(int d)
 88         {
 89             if (Find(d)!=-1)
 90             {
 91                 RemoveAt(Find(d));
 92             }
 93         }
 94         public void RemoveLast()
 95         {
 96             RemoveAt(N - 1);
 97         }
 98         /// 
 99         /// 清空数组
100         /// 
101         public void Clear()
102         {
103             for (int i = N-1; i >=0; i--)
104             {
105                 data[i] = default;
106                 N--;
107             }
108         }
109         /// 
110         /// 查询特定元素
111         /// 
112         /// 
113         /// 
114         public int Find(int d)
115         {
116             for (int i = 0; i < N; i++)
117             {
118                 if (data[i] == d)
119                 {
120                     return i;
121                 }
122             }
123             return -1;
124         }
125         /// 
126         /// 查询是否包含元素
127         /// 
128         /// 
129         /// 
130         public bool Contain(int d)
131         {
132             if (Find(d)==-1)
133             {
134                 return false;
135             }
136             return true;
137         }
138         /// 
139         /// 修改元素
140         /// 
141         /// 
142         /// 
143         public void Change(int index,int d)
144         {
145             if (index > N || index < 0)
146             {
147                 throw new ArgumentException("数组索引越界");
148             }
149             data[index] = d;
150         }
151         /// 
152         /// 扩容缩容数组
153         /// 
154         /// 
155         private void ResetCapacity(int newcapacity)
156         {
157             int[] newdata = new int[newcapacity];
158             for (int i = 0; i < N; i++)
159             {
160                 newdata[i] = data[i]; 
161             }
162             data = newdata;
163         }
164         /// 
165         /// 重写ToString方法否则不能正常输出
166         /// 
167         /// 
168         public override string ToString()
169         {
170             //实例化
171             StringBuilder res = new StringBuilder();
172             //重写格式1:输出数组元素个数以及长度
173             res.Append(string.Format("Array1:   count={0}    capacity={1}\n",N,data.Length));
174             res.Append('[');
175             //输出元素
176             for (int i = 0; i < N; i++)
177             {
178                 res.Append(data[i]);
179                 if (i!=N-1)
180                 {
181                     res.Append(',');
182                 }
183             }
184             res.Append(']'+"\n");
185             //返回
186             return res.ToString();
187         }
188     }
189 }

 泛型动态数组完整代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace DataStructrue
  8 {
  9     class Array1
 10     {
 11         /// 
 12         /// 数组的元素存储
 13         /// 
 14         private E[] data;
 15         /// 
 16         /// 数组元素数量
 17         /// 
 18         private int N;
 19 
 20         public Array1(int capacity)
 21         {
 22             data = new E[capacity];
 23             N = 0;
 24         }
 25         public Array1() : this(10) { }
 26         //与上面无参数函数等价
 27         //public Array1()
 28         //{
 29         //    data = new E[10];
 30         //    N = 0;
 31         //}
 32         public int Count { get { return N; } }
 33         public bool IsEmpty { get { return N == 0; } }
 34 
 35         /// 
 36         /// 在某位置添加新元素
 37         /// 
 38         /// 
 39         /// 
 40         public void Add(int index,E d)
 41         {
 42             if (index>N || index <0)
 43             {
 44                 throw new ArgumentException("数组索引越界");
 45             }
 46             else if (N==data.Length)
 47             {
 48                 ResetCapacity(2*N);
 49             }
 50             for (int i = N; i >= index; i--)
 51             {
 52                 if (N != data.Length-1)
 53                 {
 54                     data[i + 1] = data[i];
 55                 }
 56             }
 57             data[index] = d;
 58             N++;
 59         }
 60         /// 
 61         /// 在数组尾部添加数据
 62         /// 
 63         /// 
 64         public void Addlast(E d)
 65         {
 66             Add(N, d);
 67         }
 68         /// 
 69         /// 删除下标元素
 70         /// 
 71         /// 
 72         public E RemoveAt(int index)
 73         {
 74             if (index >= N || index < 0)
 75             {
 76                 throw new ArgumentException("数组索引越界");
 77             }
 78             E re = data[index];
 79             if (index != N - 1)
 80             {
 81                 for (int i = index; i < N; i++)
 82                 {
 83                     data[i] = data[i + 1];
 84                 }
 85             }
 86             N--;
 87             if (N==(data.Length/4))
 88             {
 89                 ResetCapacity(data.Length / 2);
 90             }
 91             data[N] = default;
 92             return re;
 93         }
 94         /// 
 95         /// 删除特定元素
 96         /// 
 97         /// 
 98         public void Remove(E d)
 99         {
100             if (Get(d)!=-1)
101             {
102                 RemoveAt(Get(d));
103             }
104         }
105         /// 
106         /// 移除最后一个数据
107         /// 
108         /// 
109         public E RemoveLast()
110         {
111             return RemoveAt(N - 1);
112         }
113         /// 
114         /// 移除最后一个数据
115         /// 
116         /// 
117         public E RemoveFirst()
118         {
119             return RemoveAt(0);
120         }
121         /// 
122         /// 清空数组
123         /// 
124         public void Clear()
125         {
126             for (int i = N-1; i >=0; i--)
127             {
128                 data[i] = default;
129                 N--;
130             }
131         }
132         /// 
133         /// 查询特定元素
134         /// 
135         /// 
136         /// 
137         public int Get(E d)
138         {
139             for (int i = 0; i < N; i++)
140             {
141                 if (data[i].Equals(d))
142                 {
143                     return i;
144                 }
145             }
146             return -1;
147         }
148         /// 
149         /// 获取第一个数据
150         /// 
151         /// 
152         public E GetFirst()
153         {
154             if (N == 0)
155             {
156                 throw new ArgumentException("数组索引越界");
157             }
158 
159             return data[0];
160         }
161         /// 
162         /// 获取最后一个数据
163         /// 
164         /// 
165         public E GetLast()
166         {
167             if (N==0)
168             {
169                 throw new ArgumentException("数组索引越界");
170             }
171 
172             return data[N-1];
173         }
174         /// 
175         /// 查询是否包含元素
176         /// 
177         /// 
178         /// 
179         public bool Contain(E d)
180         {
181             if (Get(d)==-1)
182             {
183                 return false;
184             }
185             return true;
186         }
187         /// 
188         /// 修改元素
189         /// 
190         /// 
191         /// 
192         public void Change(int index,E d)
193         {
194             if (index > N || index < 0)
195             {
196                 throw new ArgumentException("数组索引越界");
197             }
198             data[index] = d;
199         }
200         /// 
201         /// 扩容缩容数组
202         /// 
203         /// 
204         private void ResetCapacity(int newcapacity)
205         {
206             E[] newdata = new E[newcapacity];
207             for (int i = 0; i < N; i++)
208             {
209                 newdata[i] = data[i]; 
210             }
211             data = newdata;
212         }
213         /// 
214         /// 重写ToString方法否则不能正常输出
215         /// 
216         /// 
217         public override string ToString()
218         {
219             //实例化
220             StringBuilder res = new StringBuilder();
221             //重写格式1:输出数组元素个数以及长度
222             //res.Append(string.Format("Array1:   count={0}    capacity={1}\n",N,data.Length));
223             res.Append('[');
224             //输出元素
225             for (int i = 0; i < N; i++)
226             {
227                 res.Append(data[i]);
228                 if (i!=N-1)
229                 {
230                     res.Append(',');
231                 }
232             }
233             res.Append(']');
234             //返回
235             return res.ToString();
236         }
237     }
238 }