public class XList<T> : IEnumerable, IEnumerator { #region List 简单实现 /// <summary> /// 存储数据 数组 /// </summary> private T[] _items; /// <summary> /// 默认容量 /// </summary> private const int DefaultLength = 4; /// <summary> /// 插入最后一个元素索引(初始化时没有元素,所以为 -1) /// </summary> private int _lastIndex = -1; /// <summary> /// 无参构造 /// </summary> public XList() { _items = new T[DefaultLength]; } /// <summary> /// 带初始容量的构造 /// </summary> /// <param name="length"></param> public XList(int length) { if (length <= 0) { throw new Exception("长度必须大于0"); } _items = new T[length]; } /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> public T this[int index] { get { CheckIndex(index); // 验证索引越界 return _items[index]; } set { Insert(index, value); } } /// <summary> /// 添加元素 /// </summary> /// <param name="t"></param> public void Add(T t) { Insert(_lastIndex + 1, t); } /// <summary> /// 插入元素 /// </summary> /// <param name="index"></param> /// <param name="t"></param> public void Insert(int index, T t) { if (index < 0) { throw new Exception("索引不能小于0"); } KeepCapacity(); if (index <= _lastIndex) // 插入位置已有元素 { T[] temp = new T[Capacity]; for (int i = 0; i < index; i++) { temp[i] = _items[i]; } temp[index] = t; for (int i = index; i <= _lastIndex; i++) { temp[i + 1] = _items[i]; } _items = temp;
_lastIndex++; } else if (index == _lastIndex + 1) // 在末尾插入 { _items[++_lastIndex] = t; } else { throw new Exception("索引越界"); } } /// <summary> /// 按 索引 移除元素 /// </summary> /// <param name="index"></param> public void Remove(int index) { CheckIndex(index); if (index != _lastIndex) // 移除元素不是最后一个元素 { T[] temp = _items; for (int i = index; i < _lastIndex; i++) { temp[i] = _items[i + 1]; } _items = temp; } // 移除元素是最后一个元素,不做处理 _lastIndex--; } /// <summary> /// 集合存储元素数量 /// </summary> /// <returns></returns> public int Count() { return _lastIndex + 1; } /// <summary> /// 清空集合 /// </summary> public void Clear() { _lastIndex = -1; } /// <summary> /// 集合 容量 /// </summary> private int Capacity { get { if (_items != null) { return _items.Length; } return -1; } } /// <summary> /// 保证容量充足,如果数组容量不够就重置大小,容量扩大一倍 /// </summary> private void KeepCapacity() { if (Capacity == -1) // 数组为 null 的情况 { _items = new T[DefaultLength]; return; } if (_lastIndex < Capacity - 1) // 容量足够 { return; } // 容量不够 int length = Capacity * 2; T[] temp = new T[length]; for (int i = 0; i < this.Capacity; i++) { temp[i] = _items[i]; } _items = temp; } /// <summary> /// 检查索引越界的情况 /// </summary> /// <param name="index"></param> private void CheckIndex(int index) { if (index > _lastIndex) { throw new Exception("索引越界"); } } #endregion #region 实现枚举 #region IEnumerable 成员 public IEnumerator GetEnumerator() { return this; } #endregion #region IEnumerator 成员 private int _position = -1; public object Current { get { return _items[_position]; } } public bool MoveNext() { if (_position < _lastIndex) { _position++; return true; } return false; } public void Reset() { _position = -1; } #endregion #endregion }
算是比较简单的 List 的实现,暂时先这样,有些地方还得完善,
知识点 : 索引器,枚举器,其他的都比较简单