1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | 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;<br> _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 的实现,暂时先这样,有些地方还得完善,
知识点 : 索引器,枚举器,其他的都比较简单
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?