C# ArrayList的用法
2012-10-29 15:05
351 查看
System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
一、优点
1. 支持自动改变大小的功能
2. 可以灵活的插入元素
3. 可以灵活的删除元素
4. 可以灵活访问元素
二、局限性
跟一般的数组比起来,速度上差些
三、添加元素
1.public virtual int Add(object value);
将对象添加到ArrayList的结尾处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
内容为abcde
2.public virtual void Insert(int index,object value);
将元素插入ArrayList的指定索引处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
结果为aaabcde
3.public virtual void InsertRange(int index,ICollectionc);
将集合中的某个元素插入ArrayList的指定索引处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayList list2=new ArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);
结果为abtttttcde
四、删除
a)public virtual void Remove(object obj);
从ArrayList中移除特定对象的第一个匹配项,注意是第一个
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
结果为bcde
2.public virtual void RemoveAt(intindex);
移除ArrayList的指定索引处的元素
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
结果为bcde
3.public virtual void RemoveRange(int index,int count);
从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
结果为ae
4.public virtual void Clear();
从ArrayList中移除所有元素。
五、排序
a)public virtual void Sort();
对ArrayList或它的一部分中的元素进行排序。
ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDown ListDropDownList1;
DropDownList1.DataBind();
结果为eabcd
ArrayList aList=new ArrayList();
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)public virtual void Reverse();
将ArrayList或它的一部分中元素的顺序反转。
ArrayList aList=new ArrayList();
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)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();
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)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();
aList.Add("a");
aList.Add("b");
一、优点
1. 支持自动改变大小的功能
2. 可以灵活的插入元素
3. 可以灵活的删除元素
4. 可以灵活访问元素
二、局限性
跟一般的数组比起来,速度上差些
三、添加元素
1.public virtual int Add(object value);
将对象添加到ArrayList的结尾处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
内容为abcde
2.public virtual void Insert(int index,object value);
将元素插入ArrayList的指定索引处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Insert(0,"aa");
结果为aaabcde
3.public virtual void InsertRange(int index,ICollectionc);
将集合中的某个元素插入ArrayList的指定索引处
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
ArrayList list2=new ArrayList();
list2.Add("tt");
list2.Add("ttt");
aList.InsertRange(2,list2);
结果为abtttttcde
四、删除
a)public virtual void Remove(object obj);
从ArrayList中移除特定对象的第一个匹配项,注意是第一个
ArrayList aList=new ArrayList();
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.Remove("a");
结果为bcde
2.public virtual void RemoveAt(intindex);
移除ArrayList的指定索引处的元素
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveAt(0);
结果为bcde
3.public virtual void RemoveRange(int index,int count);
从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
aList.Add("e");
aList.RemoveRange(1,3);
结果为ae
4.public virtual void Clear();
从ArrayList中移除所有元素。
五、排序
a)public virtual void Sort();
对ArrayList或它的一部分中的元素进行排序。
ArrayListaList=newArrayList();
aList.Add("e");
aList.Add("a");
aList.Add("b");
aList.Add("c");
aList.Add("d");
DropDownList1.DataSource=aList;//DropDown ListDropDownList1;
DropDownList1.DataBind();
结果为eabcd
ArrayList aList=new ArrayList();
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)public virtual void Reverse();
将ArrayList或它的一部分中元素的顺序反转。
ArrayList aList=new ArrayList();
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)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();
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)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();
aList.Add("a");
aList.Add("b");
相关文章推荐
- C# ArrayList的用法
- C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)
- c#:sort在非泛型类ArrayList,泛型类List<>中的用法
- c#.net ArrayList的用法
- C# 数组、ArrayList、List、Dictionary的用法与区别
- C# ArrayList的用法
- C#.net ArrayList用法
- c#.net ArrayList的用法
- C# IList, ArrayList与List的区别详解 & 简单用法举例(转)
- C# 数组、ArrayList、List、Dictionary的用法与区别
- C# ArrayList 基本用法
- C# 数组、ArrayList、List、Dictionary的用法与区别
- C#中Array与ArrayList用法及转换
- c#中集合ArrayList用法
- C# ArrayList的用法总结
- C#中Array与ArrayList用法及转换的方法
- C# IList, ArrayList与List的区别详解 & 简单用法举例(转)
- [转]:C#集合类之Array,ArrayList,List的用法
- C#之ArrayList 的用法
- C#.net ArrayList用法