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

現(xiàn)實(shí)中,為了讓一個(gè)班新加入的10個(gè)學(xué)生能跟原來(lái)的學(xué)生住在一起而把班級(jí)整體搬遷,這樣的做法顯示不合適,因?yàn)榘徇w的成本太高。但在計(jì)算機(jī)中,內(nèi)存成片區(qū)域間的拷貝成本是非常低的,這樣的解決方案是合理可行的。
但是這個(gè)解決方案還存在問(wèn)題,如果一個(gè)班級(jí)頻繁地有新學(xué)生加入,為了保證學(xué)生能住在連續(xù)的宿舍內(nèi),整個(gè)班級(jí)就不得不頻繁地搬遷。可以采用以空間換時(shí)間的做法來(lái)解決這個(gè)問(wèn)題,在學(xué)生每次搬遷時(shí),都讓班級(jí)宿舍的數(shù)量是原來(lái)的兩倍。也就是說(shuō),如果原來(lái)一個(gè)班級(jí)有4間宿舍,搬遷后就變?yōu)?間,再次搬遷則變?yōu)?6 間。
C# ArrayList正是采用上述方法來(lái)動(dòng)態(tài)改變數(shù)組大小的。C# ArrayList又被稱為動(dòng)態(tài)數(shù)組,它的存儲(chǔ)空間可以被動(dòng)態(tài)改變,同時(shí)還擁有添加、刪除元素的功能。
下面列出了C# ArrayList的部分核心代碼:
- using System;
- namespace LinearList
- {
- public class ArrayList
- {
- // 成員變量
- private const int _defaultCapacity = 4; //默認(rèn)初始容量
- private object[] _items; //用于存放元素的數(shù)組
- private int _size; //指示當(dāng)前元素個(gè)數(shù)
- //當(dāng)元素個(gè)數(shù)為零時(shí)的數(shù)組狀態(tài)
- private static readonly object[] emptyArray = new object[0];
- // 方法
- public ArrayList() //默認(rèn)構(gòu)造方法
- { //這樣做可以避免元素個(gè)數(shù)為零時(shí)的訪問(wèn)出錯(cuò)
- this._items = emptyArray;
- }
- //指定ArrayList初始容量的構(gòu)造方法
- public ArrayList(int capacity)
- {
- if (capacity < 0)
- { //當(dāng)容量參數(shù)為負(fù)數(shù)時(shí)引發(fā)異常
- throw new ArgumentOutOfRangeException("capacity",
- "為ArrayList指定的初始容量不能為負(fù)數(shù)");
- }
- //按照capacity參數(shù)指定的長(zhǎng)度的值初始化數(shù)組
- this._items = new object[capacity];
- }
- //添加一個(gè)方法
- public virtual int Add(object value)
- { //當(dāng)空間滿時(shí)
- if (this._size == this._items.Length)
- { //調(diào)整空間
- this.EnsureCapacity(this._size + 1);
- }
- this._items[this._size] = value; //添加元素
- return this._size++; //使長(zhǎng)度加1
- }
- //動(dòng)態(tài)調(diào)整數(shù)組空間
- private void EnsureCapacity(int min)
- {
- if (this._items.Length < min)
- { //空間加倍
- int num = (this._items.Length == 0) ?
- _defaultCapacity : (this._items.Length * 2);
- if (num < min)
- {
- num = min;
- }
- //調(diào)用Capacity的set訪問(wèn)器以按照num的值調(diào)整數(shù)組空間
- this.Capacity = num;
- }
- }
- //在指定索引入插入指定元素
- public virtual void Insert(int index, object value)
- {
- if ((index < 0) || (index > this._size))
- {
- throw new ArgumentOutOfRangeException("index", "索引超出范圍");
- }
- if (this._size == this._items.Length)
- { //當(dāng)空間滿時(shí)調(diào)整空間
- this.EnsureCapacity(this._size + 1);
- }
- if (index < this._size)
- { //插入點(diǎn)后面的所有元素向后移動(dòng)一位
- Array.Copy(this._items, index,
- this._items, index + 1, this._size - index);
- }
- this._items[index] = value; //插入元素
- this._size++; //使長(zhǎng)度加1
- }
- //移除指定索引的元素
- public virtual void RemoveAt(int index)
- {
- if ((index < 0) || (index >= this._size))
- {
- throw new ArgumentOutOfRangeException("index", "索引超出范圍");
- }
- this._size--; //使長(zhǎng)度減1
- if (index < this._size)
- { //使被刪除元素后的所有元素向前移動(dòng)一位
- Array.Copy(this._items, index + 1,
- this._items, index, this._size - index);
- }
- this._items[this._size] = null; //最后一位空出的元素置空
- }
- //裁減空間,使存儲(chǔ)空間正好適合元素個(gè)數(shù)
- public virtual void TrimToSize()
- {
- thisthis.Capacity = this._size;
- }
- // 屬性
- public virtual int Capacity //指示ArrayList的存儲(chǔ)空間
- {
- get
- {
- return this._items.Length;
- }
- set
- {
- if (value != this._items.Length)
- {
- if (value < this._size)
- {
- throw new ArgumentOutOfRangeException("value", "容量太小");
- }
- if (value > 0)
- { //開辟一塊新的內(nèi)存空間存儲(chǔ)元素
- object[] destinationArray = new object[value];
- if (this._size > 0)
- { //把元素搬遷到新空間內(nèi)
- Array.Copy(this._items, 0,
- destinationArray, 0, this._size);
- }
- this._items = destinationArray;
- }
- else //最小空間為_defaultCapacity所指定的數(shù)目,這里是4
- {
- this._items = new object[_defaultCapacity];
- }
- }
- }
- }
- public virtual int Count //只讀屬性,指示當(dāng)前元素個(gè)數(shù)
- {
- get
- {
- return this._size;
- }
- }
- public virtual object this[int index] //索引器
- {
- get //獲取指定索引的元素值
- {
- if ((index < 0) || (index >= this._size))
- {
- throw new ArgumentOutOfRangeException("index", "索引超出范圍");
- }
- return this._items[index];
- }
- set //設(shè)置指定索引的元素值
- {
- if ((index < 0) || (index >= this._size))
- {
- throw new ArgumentOutOfRangeException("index", "索引超出范圍");
- }
- this._items[index] = value;
- }
- }
- }
- }
【編輯推薦】
- 定義C#接口學(xué)習(xí)經(jīng)驗(yàn)
- C# ListBox學(xué)習(xí)筆記
- 操作C# Dataset介紹
- C# ODBC訪問(wèn)MySQL數(shù)據(jù)庫(kù)
- 淺析C#和Java不同點(diǎn)
當(dāng)前題目:介紹C#ArrayList
URL網(wǎng)址:http://www.dlmjj.cn/article/dpodggi.html


咨詢
建站咨詢
