我来自唐朝
System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
一.优点
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1。支持自动改变大小的功能
2。可以灵活的插入元素
3。可以灵活的删除元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
二.局限性
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
跟一般的数组比起来,速度上差些
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
三.添加元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1.publicvirtualintAdd(objectvalue);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将对象添加到ArrayList的结尾处
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
内容为abcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.publicvirtualvoidInsert(intindex,objectvalue);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将元素插入ArrayList的指定索引处
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为aaabcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.publicvirtualvoidInsertRange(intindex,ICollectionc);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将集合中的某个元素插入ArrayList的指定索引处
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为abtttttcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
四.删除
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
a)publicvirtualvoidRemove(objectobj);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从ArrayList中移除特定对象的第一个匹配项,注意是第一个
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为bcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.publicvirtualvoidRemoveAt(intindex);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
移除ArrayList的指定索引处的元素
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为bcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.publicvirtualvoidRemoveRange(intindex,intcount);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为ae
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4.publicvirtualvoidClear();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
从ArrayList中移除所有元素。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
五.排序
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
a)publicvirtualvoidSort();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
对ArrayList或它的一部分中的元素进行排序。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDownListDropDownList1;
DropDownList1.DataBind();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为eabcd
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
结果为abcde
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
b)publicvirtualvoidReverse();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将ArrayList或它的一部分中元素的顺序反转。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
六.查找
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
a)publicvirtualintIndexOf(object);
b)publicvirtualintIndexOf(object,int);
c)publicvirtualintIndexOf(object,int,int);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ArrayListaList=newArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("a");//同0
aList.Add("d");
aList.Add("e");
intnIndex=aList.LastIndexOf("a");//值为2而不是0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
g)publicvirtualboolContains(objectitem);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
确定某个元素是否在ArrayList中。包含返回true,否则返回false
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
七.其他
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1.publicvirtualintCapacity
{get;set;}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
获取或设置ArrayList可包含的元素数。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2.publicvirtualintCount
{get;}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
获取ArrayList中实际包含的元素数。
Capacity是ArrayList可以存储的元素数。Count是ArrayList中实际包含的元素数。Capacity总是大于或等于Count。如果在添加元素时,Count超过Capacity,则该列表的容量会通过自动重新分配内部数组加倍。
如果Capacity的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果Capacity被显式设置为0,则公共语言运行库将其设置为默认容量。默认容量为16。
在调用Clear后,Count为0,而此时Capacity切是默认容量16,而不是0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3.publicvirtualvoidTrimToSize();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
将容量设置为ArrayList中元素的实际数量。
如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。
若要完全清除列表中的所有元素,请在调用TrimToSize之前调用Clear方法。截去空ArrayList会将ArrayList的容量设置为默认容量,而不是零。
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)