當前位置

首頁 > 商務英語 > 計算機英語 > c中arraylist的用法

c中arraylist的用法

推薦人: 來源: 閱讀: 5.35K 次

c中arraylist的用法的用法你知道嗎?下面小編就跟你們詳細介紹下c中arraylist的用法的用法,希望對你們有用。

c中arraylist的用法

  c中arraylist的用法的用法如下:

yList類是一個特殊的陣列。通過新增和刪除元素,就可以動態改變陣列的長度。

一、優點

1. 支援自動改變大小的功能

2. 可以靈活的插入元素

3. 可以靈活的刪除元素

4. 可以靈活訪問元素

二、侷限性

跟一般的陣列比起來,速度上差些

三、新增元素

1.public virtual int Add(object value);

將物件新增到ArrayList的結尾處

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

內容為abcde

2.public virtual void Insert(int index,object value);

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

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

rt(0,"aa");

結果為aaabcde

3.public virtual void InsertRange(int index,ICollectionc);

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

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

ArrayList list2=new ArrayList();

("tt");

("ttt");

rtRange(2,list2);

結果為abtttttcde

四、刪除

a)public virtual void Remove(object obj);

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

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

ve("a");

結果為bcde

ic virtual void RemoveAt(intindex);

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

("a");

("b");

("c");

("d");

("e");

veAt(0);

結果為bcde

3.public virtual void RemoveRange(int index,int count);

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

("a");

("b");

("c");

("d");

("e");

veRange(1,3);

結果為ae

4.public virtual void Clear();

從ArrayList中移除所有元素。

五、排序

a)public virtual void Sort();

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

ArrayListaList=newArrayList();

("e");

("a");

("b");

("c");

("d");

Source=aList;//DropDown ListDropDownList1;

Bind();

結果為eabcd

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

();//排序

Source=aList;//DropDownListDropDownList1;

Bind();

結果為abcde

b)public virtual void Reverse();

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

ArrayList aList=new ArrayList();

("a");

("b");

("c");

("d");

("e");

rse();//反轉

Source=aList;//DropDownListDropDownList1;

Bind();

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

("a");

("b");

("c");

("d");

("e");

intnIndex=xOf(“a”);//1

nIndex=xOf(“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();

("a");

("b");

("a");//同0

("d");

("e");

intnIndex=IndexOf("a");//值為2而不是0

g)public virtual bool Contains(objectitem);

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

七、獲取陣列中的元素

下面以整數為例,給出獲取某個元素的值的方法

ArrayList aList=new ArrayList();

for(int i=0;i<10;i++)

(i);

for(i=0;i<10;i++)

+=(int)aList[i]+" ";//獲取的方式基本與一般的陣列相同,使用下標的方式進行

結果為:0 1 2 3 4 5 6 7 8 9

八、其他

1.public virtual intCapacity{get;set;}

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

2.public virtual intCount{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();

("a");

("b");

("c");

("d");

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

ToSize();//Count=Capacity=5;