1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace ConsoleAppMyArrayList
7 {
8 public class MyArrayList
9 {
10 //容量
11 private const int _defaultCapacity = 4;
12 //存放数组元素
13 private object[] _items;
14 //数组大小
15 private int _size;
16 //元素个数为0的数组状态
17 private static readonly object[] emptyArray = new object[0];
18
19 public MyArrayList()
20 {
21 this._items = emptyArray;
22 }
23
24 public MyArrayList( int capacity)
25 {
26 if (capacity<0)
27 {
28 throw new ArgumentOutOfRangeException(nameof(capacity),"ArrayList的容量不可为负数!");
29 }
30 this._items = new object[capacity];
31 }
32
33 //索引器
34 public virtual object this[int index]
35 {
36 get
37 {
38 if (index<0||index>=this._size)
39 {
40 throw new ArgumentOutOfRangeException(nameof(index),"索引超出范围!");
41 }
42 return this._items[index];
43 }
44
45 set
46 {
47 if (index < 0 || index >= this._size)
48 {
49 throw new ArgumentOutOfRangeException(nameof(index), "索引超出范围!");
50 }
51 this._items[index] = value;
52 }
53
54 }
55
56 //当前数组元素个数
57 public virtual int Count
58 {
59 get {return this._size ;}
60 }
61
62 //数组的容量
63 public virtual int Capacity
64 {
65 get { return this._items.Length; }
66 set
67 {
68 if (value!=this._items.Length)
69 {
70 if (value<this._size)
71 {
72 throw new ArgumentOutOfRangeException(nameof(value),"容量太小");
73 }
74 if (value > 0)
75 {//开辟新内存空间存储元素
76 object[] dest = new object[value];
77 if (this._size > 0)
78 {//搬动元素
79 Array.Copy(this._items, 0, dest, 0, this._size);
80 }
81 this._items = dest;
82 }
83 else//数组最小的空间为4
84 {
85 this._items=new object[_defaultCapacity];
86 }
87 }
88 }
89 }
90
91 //元素的添加
92 public virtual int Add(object value)
93 {//当空间已满
94 if (this._size==this._items.Length)
95 {
96 this.EnsureCapacity(this._size+1);
97 }
98 this._items[this._size] = value;
99 return this._size++;
100 }
101
102 //扩容
103 private void EnsureCapacity(int p)
104 {
105 if (this._items.Length<p)
106 {//空间加倍
107 int num = (this._items.Length == 0) ? _defaultCapacity : (this._items.Length * 2);
108 if (num < p)
109 {
110 num = p;
111 }
112 this.Capacity = num;
113 }
114 }
115
116 //指定位置插入元素
117 public virtual void Insert( int index,object value)
118 {
119 if (index<0||index>this._size)
120 {
121 throw new ArgumentOutOfRangeException(nameof(index),"索引超出范围!");
122 }
123 if (this._size==this._items.Length)
124 {
125 this.EnsureCapacity(this._size + 1);
126 }
127 if (index<this._size)
128 {
129 Array.Copy(this._items, index, this._items, index + 1, this._size - index);
130 }
131 this._items[index] = value;
132 this._size++;
133 }
134
135 //移除指定索引的元素
136 public virtual void Remove(int index)
137 {
138 if (index < 0 || index > this._size)
139 {
140 throw new ArgumentOutOfRangeException(nameof(index), "索引超出范围!");
141 }
142 this._size--;
143 if (index<this._size)
144 {
145 Array.Copy(this._items,index+1,this._items,index,this._size-index);
146 }
147 this._items[this._size]=null;
148 }
149
150 //裁剪空间
151 public virtual void TrimToSize()
152 {
153 this.Capacity = this._size;
154 }
155 }
156 }