介绍C# ArrayList

开发 后端
本文介绍C# ArrayList又被称为动态数组,它的存储空间可以被动态改变,同时还拥有添加、删除元素的功能。

C# ArrayList
如果要动态地改变数组所占用内存空间的大小,则需以数组为基础进一步抽象,以实现这个功能。

现实中,为了让一个班新加入的10个学生能跟原来的学生住在一起而把班级整体搬迁,这样的做法显示不合适,因为搬迁的成本太高。但在计算机中,内存成片区域间的拷贝成本是非常低的,这样的解决方案是合理可行的。

但是这个解决方案还存在问题,如果一个班级频繁地有新学生加入,为了保证学生能住在连续的宿舍内,整个班级就不得不频繁地搬迁。可以采用以空间换时间的做法来解决这个问题,在学生每次搬迁时,都让班级宿舍的数量是原来的两倍。也就是说,如果原来一个班级有4间宿舍,搬迁后就变为8间,再次搬迁则变为16 间。

C# ArrayList正是采用上述方法来动态改变数组大小的。C# ArrayList又被称为动态数组,它的存储空间可以被动态改变,同时还拥有添加、删除元素的功能。

下面列出了C# ArrayList的部分核心代码:

  1. using System;  
  2. namespace LinearList  
  3. {  
  4. public class ArrayList  
  5. {  
  6. // 成员变量  
  7. private const int _defaultCapacity = 4; //默认初始容量  
  8. private object[] _items; //用于存放元素的数组  
  9. private int _size; //指示当前元素个数  
  10. //当元素个数为零时的数组状态  
  11. private static readonly object[] emptyArray = new object[0];  
  12. // 方法  
  13. public ArrayList() //默认构造方法  
  14. { //这样做可以避免元素个数为零时的访问出错  
  15. this._items = emptyArray;  
  16. }  
  17. //指定ArrayList初始容量的构造方法  
  18. public ArrayList(int capacity)  
  19. {  
  20. if (capacity < 0)  
  21. { //当容量参数为负数时引发异常  
  22. throw new ArgumentOutOfRangeException("capacity",  
  23. "为ArrayList指定的初始容量不能为负数");  
  24. }  
  25. //按照capacity参数指定的长度的值初始化数组  
  26. this._items = new object[capacity];  
  27. }  
  28. //添加一个方法  
  29. public virtual int Add(object value)  
  30. { //当空间满时  
  31. if (this._size == this._items.Length)  
  32. { //调整空间  
  33. this.EnsureCapacity(this._size + 1);  
  34. }  
  35. this._items[this._size] = value; //添加元素  
  36. return this._size++; //使长度加1  
  37. }  
  38. //动态调整数组空间  
  39. private void EnsureCapacity(int min)  
  40. {  
  41. if (this._items.Length < min)  
  42. { //空间加倍  
  43. int num = (this._items.Length == 0) ?  
  44. _defaultCapacity : (this._items.Length * 2);  
  45. if (num < min)  
  46. {  
  47. num = min;  
  48. }  
  49. //调用Capacity的set访问器以按照num的值调整数组空间  
  50. this.Capacity = num;  
  51. }  
  52. }  
  53. //在指定索引入插入指定元素  
  54. public virtual void Insert(int index, object value)  
  55. {  
  56. if ((index < 0) || (index > this._size))  
  57. {  
  58. throw new ArgumentOutOfRangeException("index", "索引超出范围");  
  59. }  
  60. if (this._size == this._items.Length)  
  61. { //当空间满时调整空间  
  62. this.EnsureCapacity(this._size + 1);  
  63. }  
  64. if (index < this._size)  
  65. { //插入点后面的所有元素向后移动一位  
  66. Array.Copy(this._items, index,  
  67. this._items, index + 1, this._size - index);  
  68. }  
  69. this._items[index] = value; //插入元素  
  70. this._size++; //使长度加1  
  71. }  
  72. //移除指定索引的元素  
  73. public virtual void RemoveAt(int index)  
  74. {  
  75. if ((index < 0) || (index >= this._size))  
  76. {  
  77. throw new ArgumentOutOfRangeException("index", "索引超出范围");  
  78. }  
  79. this._size--; //使长度减1  
  80. if (index < this._size)  
  81. { //使被删除元素后的所有元素向前移动一位  
  82. Array.Copy(this._items, index + 1,  
  83. this._items, index, this._size - index);  
  84. }  
  85. this._items[this._size] = null; //最后一位空出的元素置空  
  86. }  
  87. //裁减空间,使存储空间正好适合元素个数  
  88. public virtual void TrimToSize()  
  89. {  
  90. thisthis.Capacity = this._size;  
  91. }  
  92.  
  93. // 属性  
  94. public virtual int Capacity //指示ArrayList的存储空间  
  95. {  
  96. get  
  97. {  
  98. return this._items.Length;  
  99. }  
  100. set  
  101. {  
  102. if (value != this._items.Length)  
  103. {  
  104. if (value < this._size)  
  105. {  
  106. throw new ArgumentOutOfRangeException("value", "容量太小");  
  107. }  
  108. if (value > 0)  
  109. { //开辟一块新的内存空间存储元素  
  110. object[] destinationArray = new object[value];  
  111. if (this._size > 0)  
  112. { //把元素搬迁到新空间内  
  113. Array.Copy(this._items, 0,  
  114. destinationArray, 0, this._size);  
  115. }  
  116. this._items = destinationArray;  
  117. }  
  118. else //最小空间为_defaultCapacity所指定的数目,这里是4  
  119. {  
  120. this._items = new object[_defaultCapacity];  
  121. }  
  122. }  
  123. }  
  124. }  
  125. public virtual int Count //只读属性,指示当前元素个数  
  126. {  
  127. get  
  128. {  
  129. return this._size;  
  130. }  
  131. }  
  132. public virtual object this[int index] //索引器  
  133. {  
  134. get //获取指定索引的元素值  
  135. {  
  136. if ((index < 0) || (index >= this._size))  
  137. {  
  138. throw new ArgumentOutOfRangeException("index", "索引超出范围");  
  139. }  
  140. return this._items[index];  
  141. }  
  142. set //设置指定索引的元素值  
  143. {  
  144. if ((index < 0) || (index >= this._size))  
  145. {  
  146. throw new ArgumentOutOfRangeException("index", "索引超出范围");  
  147. }  
  148. this._items[index] = value;  
  149. }  
  150. }  
  151. }  

【编辑推荐】

  1. 定义C#接口学习经验
  2. C# ListBox学习笔记
  3. 操作C# Dataset介绍
  4. C# ODBC访问MySQL数据库
  5. 浅析C#和Java不同点
责任编辑:佚名 来源: BlogJava
相关推荐

2009-08-26 11:30:16

C# Arraylis

2009-08-14 17:45:52

C# ArrayLis

2009-08-25 10:24:29

C# delegate

2009-08-10 16:30:56

C# BitmapDa

2009-09-02 17:20:50

C# Parsing

2009-08-17 16:47:51

C# Anonymou

2009-08-12 09:41:28

C# Director

2009-07-31 14:15:38

C# 构造函数

2009-08-12 15:43:02

操作C# Datase

2009-09-03 15:57:11

C# SystemMe

2009-08-04 08:48:44

C#内置特性

2009-08-18 16:45:40

C# Raw Sock

2009-08-12 15:34:40

C# DBNull

2009-08-03 17:51:43

C#引用类型

2009-09-17 18:14:05

C#动态数组

2009-09-07 15:40:06

2009-08-20 16:25:59

C# 匿名方法

2009-08-07 17:45:29

C#预处理

2009-08-07 16:10:20

C#调用API

2009-09-01 16:19:57

C# new()约束
点赞
收藏

51CTO技术栈公众号