C#自定義List類
阿新 • • 發佈:2018-12-26
程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyArrayList { public class MyArrayList { //容量 private const int _defaultCapacity = 4; //存放陣列元素 private object[] _items; //陣列大小 private int _size; //元素個數為0的陣列狀態 private static readonly object[] emptyArray = new object[0]; /// <summary> /// 解構函式(為了不引發未將物件引用到例項,所以對_items賦值) /// </summary> public MyArrayList() { this._items = emptyArray; } public MyArrayList(int capacity) { if(capacity<0) throw new ArgumentOutOfRangeException("capacity","ArrayList的容量不可為負數"); this._items = new object[capacity]; } /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> public virtual object this[int index] { get { if (index < 0 || index >= this._size) throw new ArgumentOutOfRangeException("index", "索引超出範圍"); return this._items[index]; } set { if (index < 0 || index >= this._size) throw new ArgumentOutOfRangeException("index","索引超出範圍"); this._items[index] = value; } } /// <summary> /// 獲取當前陣列元素個數 /// </summary> public virtual int Count { get { return this._size; } } /// <summary> /// 陣列的容量 /// </summary> public virtual int Capacity { get { return this._items.Length; } set { if (value != this._items.Length) { if (value < this._size) { throw new ArgumentOutOfRangeException("value", "容量太小"); } if (value > 0) { //開闢新記憶體控制元件儲存元素 object[] dest = new object[value]; if (this._size > 0) { //搬動元素 Array.Copy(this._items, 0, dest, 0, this._size); } this._items = dest; } else //陣列最小的空間為4 { this._items = new object[_defaultCapacity]; } } } } /// <summary> /// 新增元素 /// </summary> /// <param name="value"></param> /// <returns></returns> public virtual int Add(object value) { //當空間已滿 if (this._size == this._items.Length) this.EnsureCapacity(this._size + 1); this._items[this._size] = value; return this._size++; } /// <summary> /// 對陣列進行擴容 /// </summary> /// <param name="p"></param> public void EnsureCapacity(int p) { if (this._items.Length<p) { //空間加倍 int num = (this._items.Length == 0) ? _defaultCapacity : (this._items.Length * 2); if (num <p) num = p; this.Capacity = num; } } /// <summary> /// 向指定元素插入元素 /// </summary> public virtual void Insert(int index,object value) { if (index<0 || index>this._size) throw new ArgumentOutOfRangeException("index","索引超出範圍"); if (this._size == this._items.Length) this.EnsureCapacity(this._size + 1); if (index < this._size) Array.Copy(this._items, index, this._items, index + 1, this._size - index); this._items[index] = value; this._size++; } /// <summary> /// 移除指定索引的元素 /// </summary> /// <param name="index">索引</param> public virtual void Remove(int index) { if (index<0 || index>this._size) throw new ArgumentOutOfRangeException("index","索引超出範圍"); this._size--; if (index < this._size) Array.Copy(this._items, index + 1, this._items, index, this._size - index); this._items[this._size] = null; } /// <summary> /// 裁剪空間 /// </summary> public virtual void TrimToSize() { this.Capacity = this._size; } } }