自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

介紹C# ArrayList

開發(fā) 后端
本文介紹C# ArrayList又被稱為動態(tài)數(shù)組,它的存儲空間可以被動態(tài)改變,同時(shí)還擁有添加、刪除元素的功能。

C# ArrayList
如果要動態(tài)地改變數(shù)組所占用內(nèi)存空間的大小,則需以數(shù)組為基礎(chǔ)進(jìn)一步抽象,以實(shí)現(xiàn)這個(gè)功能。

現(xiàn)實(shí)中,為了讓一個(gè)班新加入的10個(gè)學(xué)生能跟原來的學(xué)生住在一起而把班級整體搬遷,這樣的做法顯示不合適,因?yàn)榘徇w的成本太高。但在計(jì)算機(jī)中,內(nèi)存成片區(qū)域間的拷貝成本是非常低的,這樣的解決方案是合理可行的。

但是這個(gè)解決方案還存在問題,如果一個(gè)班級頻繁地有新學(xué)生加入,為了保證學(xué)生能住在連續(xù)的宿舍內(nèi),整個(gè)班級就不得不頻繁地搬遷??梢圆捎靡钥臻g換時(shí)間的做法來解決這個(gè)問題,在學(xué)生每次搬遷時(shí),都讓班級宿舍的數(shù)量是原來的兩倍。也就是說,如果原來一個(gè)班級有4間宿舍,搬遷后就變?yōu)?間,再次搬遷則變?yōu)?6 間。

C# ArrayList正是采用上述方法來動態(tài)改變數(shù)組大小的。C# ArrayList又被稱為動態(tài)數(shù)組,它的存儲空間可以被動態(tài)改變,同時(shí)還擁有添加、刪除元素的功能。

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

  1. using System;  
  2. namespace LinearList  
  3. {  
  4. public class ArrayList  
  5. {  
  6. // 成員變量  
  7. private const int _defaultCapacity = 4; //默認(rèn)初始容量  
  8. private object[] _items; //用于存放元素的數(shù)組  
  9. private int _size; //指示當(dāng)前元素個(gè)數(shù)  
  10. //當(dāng)元素個(gè)數(shù)為零時(shí)的數(shù)組狀態(tài)  
  11. private static readonly object[] emptyArray = new object[0];  
  12. // 方法  
  13. public ArrayList() //默認(rèn)構(gòu)造方法  
  14. { //這樣做可以避免元素個(gè)數(shù)為零時(shí)的訪問出錯  
  15. this._items = emptyArray;  
  16. }  
  17. //指定ArrayList初始容量的構(gòu)造方法  
  18. public ArrayList(int capacity)  
  19. {  
  20. if (capacity < 0)  
  21. { //當(dāng)容量參數(shù)為負(fù)數(shù)時(shí)引發(fā)異常  
  22. throw new ArgumentOutOfRangeException("capacity",  
  23. "為ArrayList指定的初始容量不能為負(fù)數(shù)");  
  24. }  
  25. //按照capacity參數(shù)指定的長度的值初始化數(shù)組  
  26. this._items = new object[capacity];  
  27. }  
  28. //添加一個(gè)方法  
  29. public virtual int Add(object value)  
  30. { //當(dāng)空間滿時(shí)  
  31. if (this._size == this._items.Length)  
  32. { //調(diào)整空間  
  33. this.EnsureCapacity(this._size + 1);  
  34. }  
  35. this._items[this._size] = value; //添加元素  
  36. return this._size++; //使長度加1  
  37. }  
  38. //動態(tài)調(diào)整數(shù)組空間  
  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. //調(diào)用Capacity的set訪問器以按照num的值調(diào)整數(shù)組空間  
  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. { //當(dāng)空間滿時(shí)調(diào)整空間  
  62. this.EnsureCapacity(this._size + 1);  
  63. }  
  64. if (index < this._size)  
  65. { //插入點(diǎn)后面的所有元素向后移動一位  
  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. //裁減空間,使存儲空間正好適合元素個(gè)數(shù)  
  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. { //開辟一塊新的內(nèi)存空間存儲元素  
  110. object[] destinationArray = new object[value];  
  111. if (this._size > 0)  
  112. { //把元素搬遷到新空間內(nèi)  
  113. Array.Copy(this._items, 0,  
  114. destinationArray, 0, this._size);  
  115. }  
  116. this._items = destinationArray;  
  117. }  
  118. else //最小空間為_defaultCapacity所指定的數(shù)目,這里是4  
  119. {  
  120. this._items = new object[_defaultCapacity];  
  121. }  
  122. }  
  123. }  
  124. }  
  125. public virtual int Count //只讀屬性,指示當(dāng)前元素個(gè)數(shù)  
  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 //設(shè)置指定索引的元素值  
  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#接口學(xué)習(xí)經(jīng)驗(yàn)
  2. C# ListBox學(xué)習(xí)筆記
  3. 操作C# Dataset介紹
  4. C# ODBC訪問MySQL數(shù)據(jù)庫
  5. 淺析C#和Java不同點(diǎn)
責(zé)任編輯:佚名 來源: BlogJava
相關(guān)推薦

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-17 16:47:51

C# Anonymou

2009-09-02 17:20:50

C# Parsing

2009-08-10 16:30:56

C# BitmapDa

2009-08-12 09:41:28

C# Director

2009-09-03 15:57:11

C# SystemMe

2009-08-04 08:48:44

C#內(nèi)置特性

2009-07-31 14:15:38

C# 構(gòu)造函數(shù)

2009-08-12 15:34:40

C# DBNull

2009-08-12 15:43:02

操作C# Datase

2009-08-18 16:45:40

C# Raw Sock

2009-08-18 17:17:05

C#局部類型

2009-09-07 15:40:06

2009-08-21 15:16:23

C#使用指針

2009-08-26 17:31:59

C# const常量

2009-08-24 18:21:23

C# ListView

2009-09-17 18:14:05

C#動態(tài)數(shù)組

2009-09-01 16:19:57

C# new()約束
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號