ArrayList用法整理

System.Collections.ArrayList類是一個特殊的數組。通過添加和刪除元素,就可以動態改變數組的長度。

 

一.優點

1、支持自動改變大小的功能

2、可以靈活的插入元素

3、可以靈活的刪除元素

 

二.侷限性

跟一般的數組比起來,速度上差些

 

三.添加元素

1.public virtual int Add(objectvalue);

將對象添加到ArrayList的結尾處

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

 

內容爲abcde

 

2.public virtual void Insert(intindex,objectvalue);

將元素插入ArrayList的指定索引處

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Insert(0,"aa");

 

結果爲aaabcde

 

3.public virtual void InsertRange(intindex,ICollectionc);

將集合中的某個元素插入ArrayList的指定索引處

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

ArrayList list2 = new ArrayList();

list2.Add("tt");

list2.Add("ttt");

aList.InsertRange(2,list2);

 

結果爲abtttttcde

 

四.刪除

1. public virtual void Remove(objectobj);

從ArrayList中移除特定對象的第一個匹配項,注意是第一個

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Remove("a");

 

結果爲bcde

 

2. public virtual void RemoveAt(intindex);

移除ArrayList的指定索引處的元素

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveAt(0);

 

結果爲bcde

 

3.public virtual void RemoveRange(intindex,intcount);

從ArrayList中移除一定範圍的元素。Index表示索引,count表示從索引處開始的數目

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveRange(1,3);

 

結果爲ae

 

4.public virtual void Clear();

從ArrayList中移除所有元素。

 

五.排序

a) public virtual void Sort();

對ArrayList或它的一部分中的元素進行排序。

ArrayList aList = new ArrayList();

aList.Add("e");

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

DropDownList1.DataSource = aList; //DropDownListDropDownList1;

DropDownList1.DataBind();

 

結果爲eabcd

 

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Sort();//排序

DropDownList1.DataSource = aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

 

結果爲abcde

 

b) public virtual void Reverse();

將ArrayList或它的一部分中元素的順序反轉。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Reverse();//反轉

DropDownList1.DataSource = aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

 

結果爲edcba

 

六.查找

a) public virtual int IndexOf(object);

b) public virtual int IndexOf(object,int);

c) public virtual int IndexOf(object,int,int);

返回ArrayList或它的一部分中某個值的第一個匹配項的從零開始的索引。沒找到返回-1。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

 aList.Add("c");

aList.Add("d");

aList.Add("e");

intnIndex = aList.IndexOf(“a”);//1

nIndex = aList.IndexOf(“p”);//沒找到,-1

 

d) public virtual int LastIndexOf(object);

e) public virtual int LastIndexOf(object,int);

f) public virtual int LastIndexOf(object,int,int);

返回ArrayList或它的一部分中某個值的最後一個匹配項的從零開始的索引。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("a");//同0

aList.Add("d");

aList.Add("e");

intnIndex = aList.LastIndexOf("a");//值爲2而不是0

 

g) public virtual bool Contains(objectitem);

確定某個元素是否在ArrayList中。包含返回true,否則返回false

 

七.其他

1.public virtual int Capacity{get;set;}

獲取或設置ArrayList可包含的元素數。

 

2.public virtual int Count{get;}

獲取ArrayList中實際包含的元素數。 Capacity是ArrayList可以存儲的元素數。Count是ArrayList中實際包含的元素數。Capacity總是大於或等於Count。如果在添加元素時,Count超過Capacity,則該列表的容量會通過自動重新分配內部數組加倍。 如果Capacity的值顯式設置,則內部數組也需要重新分配以容納指定的容量。如果Capacity被顯式設置爲0,則公共語言運行庫將其設置爲默認容量。默認容量爲16。 在調用Clear後,Count爲0,而此時Capacity切是默認容量16,而不是0

 

3.public virtual void TrimToSize();

將容量設置爲ArrayList中元素的實際數量。 如果不向列表中添加新元素,則此方法可用於最小化列表的內存系統開銷。 若要完全清除列表中的所有元素,請在調用TrimToSize之前調用Clear方法。截去空ArrayList會將ArrayList的容量設置爲默認容量,而不是零。

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");//Count=5,Capacity=16,

aList.TrimToSize();//Count=Capacity=5;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章