C#中ArrayList的使用方法
更新時(shí)間:2013年12月09日 15:42:48 作者:
這篇文章主要介紹了
System.Collections.ArrayList類(lèi)是一個(gè)特殊的數(shù)組。通過(guò)添加和刪除元素,就可以動(dòng)態(tài)改變數(shù)組的長(zhǎng)度。
一.優(yōu)點(diǎn)
1。支持自動(dòng)改變大小的功能
2??梢造`活的插入元素
3。可以靈活的刪除元素
二.局限性
跟一般的數(shù)組比起來(lái),速度上差些
三.添加元素
1.publicvirtualintAdd(objectvalue);
將對(duì)象添加到ArrayList的結(jié)尾處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
內(nèi)容為abcde
2.publicvirtualvoidInsert(intindex,objectvalue);
將元素插入ArrayList的指定索引處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
結(jié)果為aaabcde
3.publicvirtualvoidInsertRange(intindex,ICollectionc);
將集合中的某個(gè)元素插入ArrayList的指定索引處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayListlist2=newArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);
結(jié)果為abtttttcde
四.刪除
a)publicvirtualvoidRemove(objectobj);
從ArrayList中移除特定對(duì)象的第一個(gè)匹配項(xiàng),注意是第一個(gè)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
結(jié)果為bcde
2.publicvirtualvoidRemoveAt(intindex);
移除ArrayList的指定索引處的元素
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
結(jié)果為bcde
3.publicvirtualvoidRemoveRange(intindex,intcount);
從ArrayList中移除一定范圍的元素。Index表示索引,count表示從索引處開(kāi)始的數(shù)目
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
結(jié)果為ae
4.publicvirtualvoidClear();
從ArrayList中移除所有元素。
五.排序
a)publicvirtualvoidSort();
對(duì)ArrayList或它的一部分中的元素進(jìn)行排序。
ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為eabcd
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Sort();//排序
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為abcde
b)publicvirtualvoidReverse();
將ArrayList或它的一部分中元素的順序反轉(zhuǎn)。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Reverse();//反轉(zhuǎn)
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為edcba
六.查找
a)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);
返回ArrayList或它的一部分中某個(gè)值的第一個(gè)匹配項(xiàng)的從零開(kāi)始的索引。沒(méi)找到返回-1。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
intnIndex=aList.IndexOf(“a”);//1
nIndex=aList.IndexOf(“p”);//沒(méi)找到,-1
d)publicvirtualintLastIndexOf(object);
e)publicvirtualintLastIndexOf(object,int);
f)publicvirtualintLastIndexOf(object,int,int);
返回ArrayList或它的一部分中某個(gè)值的最后一個(gè)匹配項(xiàng)的從零開(kāi)始的索引。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a");//同0
aList.Add("d");
aList.Add("e");
intnIndex=aList.LastIndexOf("a");//值為2而不是0
g)publicvirtualboolContains(objectitem);
確定某個(gè)元素是否在ArrayList中。包含返回true,否則返回false
七.其他
1.publicvirtualintCapacity{get;set;}
獲取或設(shè)置ArrayList可包含的元素?cái)?shù)。
2.publicvirtualintCount{get;}
獲取ArrayList中實(shí)際包含的元素?cái)?shù)。
Capacity是ArrayList可以存儲(chǔ)的元素?cái)?shù)。Count是ArrayList中實(shí)際包含的元素?cái)?shù)。Capacity總是大于或等于Count。如果在添加元素時(shí),Count超過(guò)Capacity,則該列表的容量會(huì)通過(guò)自動(dòng)重新分配內(nèi)部數(shù)組加倍。
如果Capacity的值顯式設(shè)置,則內(nèi)部數(shù)組也需要重新分配以容納指定的容量。如果Capacity被顯式設(shè)置為0,則公共語(yǔ)言運(yùn)行庫(kù)將其設(shè)置為默認(rèn)容量。默認(rèn)容量為16。
在調(diào)用Clear后,Count為0,而此時(shí)Capacity切是默認(rèn)容量16,而不是0
3.publicvirtualvoidTrimToSize();
將容量設(shè)置為ArrayList中元素的實(shí)際數(shù)量。
如果不向列表中添加新元素,則此方法可用于最小化列表的內(nèi)存系統(tǒng)開(kāi)銷(xiāo)。
若要完全清除列表中的所有元素,請(qǐng)?jiān)谡{(diào)用TrimToSize之前調(diào)用Clear方法。截去空ArrayList會(huì)將ArrayList的容量設(shè)置為默認(rèn)容量,而不是零。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");//Count=5,Capacity=16,
aList.TrimToSize();//Count=Capacity=5;
一.優(yōu)點(diǎn)
1。支持自動(dòng)改變大小的功能
2??梢造`活的插入元素
3。可以靈活的刪除元素
二.局限性
跟一般的數(shù)組比起來(lái),速度上差些
三.添加元素
1.publicvirtualintAdd(objectvalue);
將對(duì)象添加到ArrayList的結(jié)尾處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
內(nèi)容為abcde
2.publicvirtualvoidInsert(intindex,objectvalue);
將元素插入ArrayList的指定索引處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
結(jié)果為aaabcde
3.publicvirtualvoidInsertRange(intindex,ICollectionc);
將集合中的某個(gè)元素插入ArrayList的指定索引處
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayListlist2=newArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);
結(jié)果為abtttttcde
四.刪除
a)publicvirtualvoidRemove(objectobj);
從ArrayList中移除特定對(duì)象的第一個(gè)匹配項(xiàng),注意是第一個(gè)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
結(jié)果為bcde
2.publicvirtualvoidRemoveAt(intindex);
移除ArrayList的指定索引處的元素
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
結(jié)果為bcde
3.publicvirtualvoidRemoveRange(intindex,intcount);
從ArrayList中移除一定范圍的元素。Index表示索引,count表示從索引處開(kāi)始的數(shù)目
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
結(jié)果為ae
4.publicvirtualvoidClear();
從ArrayList中移除所有元素。
五.排序
a)publicvirtualvoidSort();
對(duì)ArrayList或它的一部分中的元素進(jìn)行排序。
ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為eabcd
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Sort();//排序
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為abcde
b)publicvirtualvoidReverse();
將ArrayList或它的一部分中元素的順序反轉(zhuǎn)。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Reverse();//反轉(zhuǎn)
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
結(jié)果為edcba
六.查找
a)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);
返回ArrayList或它的一部分中某個(gè)值的第一個(gè)匹配項(xiàng)的從零開(kāi)始的索引。沒(méi)找到返回-1。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
intnIndex=aList.IndexOf(“a”);//1
nIndex=aList.IndexOf(“p”);//沒(méi)找到,-1
d)publicvirtualintLastIndexOf(object);
e)publicvirtualintLastIndexOf(object,int);
f)publicvirtualintLastIndexOf(object,int,int);
返回ArrayList或它的一部分中某個(gè)值的最后一個(gè)匹配項(xiàng)的從零開(kāi)始的索引。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a");//同0
aList.Add("d");
aList.Add("e");
intnIndex=aList.LastIndexOf("a");//值為2而不是0
g)publicvirtualboolContains(objectitem);
確定某個(gè)元素是否在ArrayList中。包含返回true,否則返回false
七.其他
1.publicvirtualintCapacity{get;set;}
獲取或設(shè)置ArrayList可包含的元素?cái)?shù)。
2.publicvirtualintCount{get;}
獲取ArrayList中實(shí)際包含的元素?cái)?shù)。
Capacity是ArrayList可以存儲(chǔ)的元素?cái)?shù)。Count是ArrayList中實(shí)際包含的元素?cái)?shù)。Capacity總是大于或等于Count。如果在添加元素時(shí),Count超過(guò)Capacity,則該列表的容量會(huì)通過(guò)自動(dòng)重新分配內(nèi)部數(shù)組加倍。
如果Capacity的值顯式設(shè)置,則內(nèi)部數(shù)組也需要重新分配以容納指定的容量。如果Capacity被顯式設(shè)置為0,則公共語(yǔ)言運(yùn)行庫(kù)將其設(shè)置為默認(rèn)容量。默認(rèn)容量為16。
在調(diào)用Clear后,Count為0,而此時(shí)Capacity切是默認(rèn)容量16,而不是0
3.publicvirtualvoidTrimToSize();
將容量設(shè)置為ArrayList中元素的實(shí)際數(shù)量。
如果不向列表中添加新元素,則此方法可用于最小化列表的內(nèi)存系統(tǒng)開(kāi)銷(xiāo)。
若要完全清除列表中的所有元素,請(qǐng)?jiān)谡{(diào)用TrimToSize之前調(diào)用Clear方法。截去空ArrayList會(huì)將ArrayList的容量設(shè)置為默認(rèn)容量,而不是零。
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");//Count=5,Capacity=16,
aList.TrimToSize();//Count=Capacity=5;
相關(guān)文章
WPF自定義實(shí)現(xiàn)雷達(dá)圖控件的示例詳解
雷達(dá)圖用于表示不同內(nèi)容的占比關(guān)系,在項(xiàng)目中有廣泛的應(yīng)用,但是目前未曾有封裝良好的雷達(dá)圖控件,所以本文分享了如何封裝一個(gè)通用的雷達(dá)圖控件,希望對(duì)大家有所幫助2023-08-08
C#8.0 中開(kāi)啟默認(rèn)接口實(shí)現(xiàn)方法
這篇文章主要介紹了C#8.0 中開(kāi)啟默認(rèn)接口實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧的相關(guān)資料2019-05-05
利用C#實(shí)現(xiàn)AOP常見(jiàn)的幾種方法詳解
AOP面向切面編程(Aspect Oriented Programming),是通過(guò)預(yù)編譯方式和運(yùn)行期動(dòng)態(tài)代理實(shí)現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用C#實(shí)現(xiàn)AOP常見(jiàn)的幾種方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09
Unity3D獲取當(dāng)前鍵盤(pán)按鍵及Unity3D鼠標(biāo)、鍵盤(pán)的基本操作
這篇文章主要介紹了Unity3D獲取當(dāng)前鍵盤(pán)按鍵及Unity3D鼠標(biāo)、鍵盤(pán)的基本操作的相關(guān)資料,需要的朋友可以參考下2015-11-11

