C#中ArrayList的使用方法

 更新时间:2020年6月25日 11:39  点击:1698
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; 
[!--infotagslink--]

相关文章

  • C#中数组、ArrayList、List、Dictionary的用法与区别浅析(存取数据)

    在工作中经常遇到C#数组、ArrayList、List、Dictionary存取数据,但是该选择哪种类型进行存储数据呢?很迷茫,今天小编抽空给大家整理下这方面的内容,需要的朋友参考下吧...2020-06-25
  • C# 中 Array和 ArrayList详解及区别

    这篇文章主要介绍了C# 中 Array和 ArrayList详解及区别的相关资料,需要的朋友可以参考下...2020-06-25
  • C#中数组、ArrayList和List三者的区别详解

    这篇文章主要介绍了C#中数组、ArrayList和List三者的区别详解,对于三者之间的区别想要了解的可以进来了解一下。...2020-06-25
  • C#中Arraylist的sort函数用法实例分析

    这篇文章主要介绍了C#中Arraylist的sort函数用法,较为详细的分析了ArrayList的sort函数的功能、定义及具体使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • Java泛型模拟scala实现自定义ArrayList方式

    这篇文章主要介绍了Java泛型模拟scala实现自定义ArrayList方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-11
  • 解析C#中[],List,Array,ArrayList的区别及应用

    本篇文章主要是对C#中[],List,Array,ArrayList的区别及应用进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助...2020-06-25
  • Repeater控件分别绑定数组和ArrayList实现思路

    在后台用DataSource绑上数据源(数组或ArrayList)在调用DataBind()方法,在前台调用%# GetDataItem()%,感兴趣的朋友可以了解下啊,望本文可以巩固你的数据绑定知识...2021-09-22
  • Java中ArrayList集合的常用方法大全

    这篇文章主要给大家介绍了关于Java中ArrayList集合的常用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-01-25
  • C#生成随机ArrayList的方法

    这篇文章主要介绍了C#生成随机ArrayList的方法,实例分析了C#中ArrayList的相关操作技巧,需要的朋友可以参考下...2020-06-25
  • C#中数组、ArrayList和List三者的区别详解及实例

    这篇文章主要介绍了C#中数组、ArrayList和List三者的区别详解及实例的相关资料,需要的朋友可以参考下...2020-06-25
  • c# ArrayList的使用方法小总结

    arraylist是接口list的实现类,所以在使用过程中比较推荐使用list接口来实现,arraylist在程序开发过程中应用非常广泛,接下来,脚本之家的小编给大家总结了arraylist的使用,有需要的朋友可以参考下...2020-06-25
  • C#入门教程之集合ArrayList用法详解

    这篇文章主要介绍了C#入门教程之集合ArrayList用法,结合具体实例分析了C#中集合的概念、功能、创建与使用方法,需要的朋友可以参考下...2020-06-25
  • C#中数组Array,ArrayList,泛型List详细对比

    关于数组Array,ArrayList,泛型List,简单的说数组就是值对象,它存储数据元素类型的值的一系列位置.Arraylist和list可以提供添加,删除,等操作的数据. 具体如何进行选择使用呢,我们来详细探讨下...2020-06-25
  • 浅析C#中数组,ArrayList与List对象的区别

    在C#中,当我们想要存储一组对象的时候,就会想到用数组,ArrayList,List这三个对象了。那么这三者到底有什么样的区别呢...2020-06-25
  • 浅析 ArrayList 和 LinkedList 有什么区别

    ArrayList 和 LinkedList 有什么区别,是面试官非常喜欢问的一个问题。今天通过本文给大家详细介绍下,感兴趣的朋友跟随小编一起看看吧...2020-10-14
  • Java Vector和ArrayList的异同分析及实例讲解

    在本篇文章里小编给大家整理的是一篇关于Java Vector和ArrayList的异同分析及实例讲解内容,有兴趣的朋友们可以学习参考下。...2021-01-19
  • 详解ArrayList的扩容机制

    ArrayList基于动态数组实现,在添加和删除的时候存在扩容和缩容这样重新规划数组大小的机制。在ArrayList中,维护Object[] elementData数组来管理元素,但是ArrayList是动态可变的,所以elementData数组长度并不代表ArrayList实际元素个数,所以使用size显示实际元素个数...2021-06-21
  • Java8 ArrayList之forEach的使用

    这篇文章主要介绍了Java8 ArrayList之forEach的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-20
  • 轻松学习C#的ArrayList类

    轻松学习C#的ArrayList类,对C#的ArrayList类感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的ArrayList类。...2020-06-25
  • Java多线程高并发中解决ArrayList与HashSet和HashMap不安全的方案

    ArrayList实现了可变大小的数组。它允许所有元素,包括null。ArrayList没有同步,HashMap和Hashtable类似,不同之处在于HashMap是非同步的,并且允许null,关于HashSet有一件事应该牢记,即就条目数和容量之和来讲,迭代是线性的,接下来让我们详细来了解吧...2021-11-16