当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ArrayList用法

 ArrayList用法

点击次数:17 次 发布日期:2008-11-26 12:17:36 作者:源代码网
源代码网推荐      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;
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华