C#.net ArrayList用法

來源:互聯網
上載者:User

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

一.優點

1。支援自動改變大小的功能
2。可以靈活的插入元素
3。可以靈活的刪除元素

二.局限性

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

三.添加元素

1.publicvirtualintAdd(objectvalue);

將對象添加到ArrayList的結尾處

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
內容為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");

結果為aaabcde

3.publicvirtualvoidInsertRange(intindex,ICollectionc);

將集合中的某個元素插入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);

結果為abtttttcde

四.刪除

a)publicvirtualvoidRemove(objectobj);

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

ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");

結果為bcde

2.publicvirtualvoidRemoveAt(intindex);

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

aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);

結果為bcde

3.publicvirtualvoidRemoveRange(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.publicvirtualvoidClear();

從ArrayList中移除所有元素。

五.排序

a)publicvirtualvoidSort();

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

ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();

結果為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();

結果為abcde

b)publicvirtualvoidReverse();

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

ArrayListaList=newArrayList();
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)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);

返回ArrayList或它的一部分中某個值的第一個匹配項的從零開始的索引。沒找到返回-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”);//沒找到,-1
d)publicvirtualintLastIndexOf(object);
e)publicvirtualintLastIndexOf(object,int);
f)publicvirtualintLastIndexOf(object,int,int);

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

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);

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

七.其他

1.publicvirtualintCapacity{get;set;}

擷取或設定ArrayList可包含的元素數。

2.publicvirtualintCount{get;}

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

3.publicvirtualvoidTrimToSize();

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

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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.