• C# 自定义List


    目录

    一、需求

    二、List 常用功能

    三、自定义List

    四、测试

    1.Add

    2.Clear

    3.Contains

    4.IndexOf

    5.Insert

    6.Remove

    7.RemoveAt

    结束


    一、需求

    微软官方的 List 在命名空间 System.Collections.Generic 中,在平时的开发中 List 用的特别多,在用的时候我们基本不会考虑在 List 中内部是怎么写的,于是,我也写了一个List,想看看是否能实现和微软官方一样的功能,当然,不是说为了和微软比比谁写的好,也没那个必要,原创轮子等于白费功夫,微软的API基本已经优化的很好了,直接拿来用就行了,我写这篇文章目的只是为了更了解 List 内部的构造,提升自己 C# 基础,功能当然不可能有官方那么多。

    二、List 常用功能

    Capacity

            用于获取或设置List可容纳元素的数量。当数量超过容量时,这个值会自动增长。您可以设置这个值以减少容量,也可以调用trin()方法来减少容量以适合实际的元素数目。

    Count

            获取List中当前元素数量

    Item

            通过指定索引获取或设置元素。对于List类来说,它是一个索引器。

    Add 

            在List中添加一个对象

    AddRange

            在List尾部添加实现了ICollection接口的多个元素

    BinarySearch 

            用于在排序的List内使用二分查找来定位指定元素.

    Clear

            移除List所有元素

    Contains

            测试一个元素是否在List内

    CopyTo

            把一个List拷贝到一维数组

    Exists

            测试一个元素是否在List内

    Find 

            查找并返回List内的出现的第一个匹配元素

    FindAll

            查找并返回List内的所有匹配元素

    GetEnumerator

            返回一个用于迭代List的枚举器

    Getrange

            拷贝指定范围的元素到新的List内

    IndexOf

            查找并返回每一个匹配元素的索引

    Insert

            在List内插入一个元素

    InsertRange

            在List内插入一组元素

    LastIndexOf

            查找并返回最后一个匹配元素的索引

    Remove

            移除与指定元素匹配的第一个元素

    RemoveAt

            移除指定索引的元素

    RemoveRange

            移除指定范围的元素

    Reverse

            反转List内元素的顺序

    Sort 

            对List内的元素进行排序

    ToArray

            把List内的元素拷贝到一个新的数组内

    三、自定义List

    自定义 List 如下,我将常用的功能封装了一下

    1. public class TList<T>
    2. {
    3. private const int _defaultCapacity = 4;
    4. private static readonly T[] _emptyArray;
    5. private T[] _items;
    6. private int _size;
    7. private int _version;
    8. public int Capacity
    9. {
    10. get => this._items.Length;
    11. set
    12. {
    13. if (value < this._size)
    14. {
    15. throw new ArgumentOutOfRangeException("容量太小");
    16. }
    17. if (value != this._items.Length)
    18. {
    19. if (value > 0)
    20. {
    21. T[] destinationArray = new T[value];
    22. if (this._size > 0)
    23. {
    24. Array.Copy(this._items, 0, destinationArray, 0, this._size);
    25. }
    26. this._items = destinationArray;
    27. }
    28. else
    29. {
    30. this._items = _emptyArray;
    31. }
    32. }
    33. }
    34. }
    35. public int Count => this._size;
    36. public T this[int index]
    37. {
    38. get
    39. {
    40. if (index >= this._size)
    41. {
    42. throw new ArgumentOutOfRangeException("index不能大于等于数组的长度");
    43. }
    44. return this._items[index];
    45. }
    46. set
    47. {
    48. if (index >= this._size)
    49. {
    50. throw new ArgumentOutOfRangeException("index不能大于等于数组的长度");
    51. }
    52. this._items[index] = value;
    53. this._version++;
    54. }
    55. }
    56. ///
    57. /// 添加对象到 List 中
    58. ///
    59. ///
    60. ///
    61. public int Add(object value)
    62. {
    63. if (this._size == this._items.Length)
    64. {
    65. this.EnsureCapacity(this._size + 1);
    66. }
    67. int index = this._size;
    68. this._size = index + 1;
    69. this._items[index] = (T)value;
    70. this._version++;
    71. return _size;
    72. }
    73. ///
    74. /// 从 List 中移除所有项
    75. ///
    76. public void Clear()
    77. {
    78. if (this._size > 0)
    79. {
    80. Array.Clear(this._items, 0, this._size);
    81. this._size = 0;
    82. }
    83. this._version++;
    84. }
    85. ///
    86. /// List 中是否包含指定的值
    87. ///
    88. ///
    89. ///
    90. public bool Contains(object item)
    91. {
    92. if (item == null)
    93. {
    94. for (int j = 0; j < this._size; j++)
    95. {
    96. if (this._items[j] == null)
    97. {
    98. return true;
    99. }
    100. }
    101. return false;
    102. }
    103. for (int i = 0; i < this._size; i++)
    104. {
    105. if (_items[i].Equals(item))
    106. {
    107. return true;
    108. }
    109. }
    110. return false;
    111. }
    112. ///
    113. /// 拷贝数组到 List 中指定的位置
    114. ///
    115. ///
    116. ///
    117. public void CopyTo(T[] array, int arrayIndex)
    118. {
    119. Array.Copy(this._items, 0, array, arrayIndex, this._size);
    120. }
    121. ///
    122. /// 获取 List 中特定项的索引
    123. ///
    124. ///
    125. ///
    126. public int IndexOf(object value)
    127. {
    128. return Array.IndexOf(this._items, (T)value, 0, this._size);
    129. }
    130. ///
    131. /// 从 List 中的指定索引处插入一个值
    132. ///
    133. ///
    134. ///
    135. public void Insert(int index, object value)
    136. {
    137. if (index > this._size)
    138. {
    139. throw new ArgumentOutOfRangeException("index不能大于数组长度");
    140. }
    141. if (this._size == this._items.Length)
    142. {
    143. this.EnsureCapacity(this._size + 1);
    144. }
    145. if (index < this._size)
    146. {
    147. Array.Copy(this._items, index, this._items, index + 1, this._size - index);
    148. }
    149. this._items[index] = (T)value;
    150. this._size++;
    151. this._version++;
    152. }
    153. ///
    154. /// 从 List 中移除特定对象的第一个匹配项
    155. ///
    156. ///
    157. ///
    158. public bool Remove(T item)
    159. {
    160. int index = this.IndexOf(item);
    161. if (index >= 0)
    162. {
    163. this.RemoveAt(index);
    164. return true;
    165. }
    166. return false;
    167. }
    168. ///
    169. /// 移除位于指定索引处的 List 项。
    170. ///
    171. ///
    172. public void RemoveAt(int index)
    173. {
    174. if (index >= this._size)
    175. {
    176. throw new ArgumentOutOfRangeException("index不能大于等于数组的长度");
    177. }
    178. this._size--;
    179. if (index < this._size)
    180. {
    181. Array.Copy(this._items, index + 1, this._items, index, this._size - index);
    182. }
    183. this._items[this._size] = default(T);
    184. this._version++;
    185. }
    186. private void EnsureCapacity(int min)
    187. {
    188. if (this._items.Length < min)
    189. {
    190. int num = (this._items.Length == 0) ? 4 : (this._items.Length * 2);
    191. if (num > 0x7fefffff)
    192. {
    193. num = 0x7fefffff;
    194. }
    195. if (num < min)
    196. {
    197. num = min;
    198. }
    199. this.Capacity = num;
    200. }
    201. }
    202. static TList()
    203. {
    204. _emptyArray = new T[0];
    205. }
    206. public TList()
    207. {
    208. this._items = _emptyArray;
    209. }
    210. public TList(int capacity)
    211. {
    212. if (capacity < 0)
    213. {
    214. throw new ArgumentOutOfRangeException("List长度不能小于0");
    215. }
    216. if (capacity == 0)
    217. {
    218. this._items = _emptyArray;
    219. }
    220. else
    221. {
    222. this._items = new T[capacity];
    223. }
    224. }
    225. }

      

    四、测试

    1.Add

    功能:向 List 内添加对象

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. Console.WriteLine("长度:" + list.Count);
    12. Console.ReadKey();
    13. }
    14. }
    15. }

    输出

    2.Clear

    功能:清除 List 内所有的对象

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. Console.WriteLine("长度:" + list.Count);
    12. list.Clear();
    13. Console.WriteLine("长度:" + list.Count);
    14. Console.ReadKey();
    15. }
    16. }
    17. }

    输出

    3.Contains

    功能:LIst 是否包含指定的值

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. bool res = list.Contains(15);
    12. Console.WriteLine("是否包含:" + res);
    13. Console.ReadKey();
    14. }
    15. }
    16. }

    输出

    4.IndexOf

    功能:获取 List 中特定项的索引

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. int index = list.IndexOf(23);
    12. Console.WriteLine("对应索引:" + index);
    13. Console.ReadKey();
    14. }
    15. }
    16. }

    输出

    5.Insert

    功能:从 List 中的指定索引处插入一个值

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. //Console.WriteLine("长度:" + list.Count);
    12. list.Insert(1, 45);
    13. for (int i = 0; i < list.Count; i++)
    14. {
    15. Console.WriteLine(list[i]);
    16. }
    17. Console.ReadKey();
    18. }
    19. }
    20. }

    输出

    6.Remove

    功能:从 List 中移除特定对象的一个匹配项

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. list.Add(45);
    12. bool res = list.Remove(23);
    13. Console.WriteLine("移除结果:" + res);
    14. for (int i = 0; i < list.Count; i++)
    15. {
    16. Console.WriteLine(list[i]);
    17. }
    18. Console.ReadKey();
    19. }
    20. }
    21. }

    输出

    7.RemoveAt

    功能:移除位于指定索引处的 List 项

    1. namespace 计算1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. TList<int> list = new TList<int>();
    8. list.Add(15);
    9. list.Add(62);
    10. list.Add(23);
    11. list.Add(45);
    12. list.RemoveAt(2);
    13. for (int i = 0; i < list.Count; i++)
    14. {
    15. Console.WriteLine(list[i]);
    16. }
    17. Console.ReadKey();
    18. }
    19. }
    20. }

    输出

    结束

    如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢

    end

  • 相关阅读:
    【后端的讲解】
    HTML 绝对定位
    【flask扩展】使用Flask-Mail发送邮件
    栈和队列相关的一些问题
    《软件方法》2023版第1章(09)基本共识上的沟通,SysML
    【重识云原生】第六章容器6.1.12节——Docker网络模型设计
    Unity C# 网络学习(八)——WWW
    springboot建筑造价师资格考试应试网站设计与实现毕业设计源码260839
    SQL server中merge语句添加where条件
    Grid 布局
  • 原文地址:https://blog.csdn.net/qq_38693757/article/details/126263502