System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
2011-03-26 13:33
846 查看
一.优点 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;
相关文章推荐
- 动态数组,数组初始化,数组内存释放,向数组中添加一个元素,向数组中添加多个元素,数组打印,顺序查找,二分查找,查找数组并返回地址,冒泡排序,改变数组中某个元素的值,删除一个数值,删除所有,查找含有
- 不改变原数组,添加或删除一个元素(js)
- 在一个有序的数组中添加和删除元素不改变有序结构
- 如何在javascript数组中删除一个元素并改变数组长度
- splice() 方法通过删除现有元素和/或添加新元素来更改一个数组的内容。
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中最大值
- 通过分类为数组添加一个倒序的一个方法. 比如: 数组中元素为 @”aa”, @”bb”, @”cc”, @”dd”, @”ee”, 倒序完之后为: @”ee”, @”dd”,@”cc”,@”bb”,@
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- 通过两个数组字符串。各选取一个字符串元素。使其长度绝对值最大。求最大绝对值
- 实现在一个数组指定位置添加元素和删除元素的功能
- 提高篇项目1.3—随机数创建一个长度为16的整型数组a并初始化,删除数组中所有能被3整除的元素
- // 1、输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- 输入一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- 1,随机一个数组长度,动态创建数组,所有元素随机生成,输出元素中的最大值
- 在一个长度为10的整形数组中,已经放置好了任意10个整数。要求用户任意输入 一个整数,判断该数字是否在数组中。如果在,删除数组中该元素,并把后面的 元素往前挪。(让空出的位置在最后)
- 建立一个学生结构数组来记录学生信息(学号ID,姓名和C语言成绩),要求动态建立一个结构数组,数组长度从键盘输入,自行给数组元素赋值并打印学生信息,最后不要忘记释放内存。 结构st
- JavaScript动态改变HTML页面元素例如添加或删除
- 给数组添加一个根据指定下标删除元素的方法、得到0-100的随机数不重复(js)、得到外联样式的css样式值
- 在一个长度为10的整形数组中,前9个元素是{12,23,34,45,56,67,78,89,90}。 现在要求输入一个整数,把它放到数组中正确的位置当中。(不能删除已有元素)
- php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)