C#中ArrayList的使用方法
发布时间:2023-01-26 10:20:47
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;


猜你喜欢
- 本文实例讲述了C#创建windows系统用户的方法。分享给大家供大家参考。具体如下:下面的代码可以通过c#创建一个windows的本地系统账
- 如题,记录一些平常开发用的pom文件细节1.使用parent父类引用,解决依赖版本号不确定时自动匹配的问题<parent> &n
- 本文实例讲述了Java ThreadLocal类应用。分享给大家供大家参考,具体如下:一 点睛ThreadLocal,是Thread Loc
- 混乱的URI编码 JavaScript中编码有三种方法:escape、encodeURI、encodeURIComponent C#中编码主
- 前述园子里有许多人对log4net这款开源的日志记录控件有很多介绍。在这里个人再做一次总结,希望对以后有所帮助,需要的时候可以直接使用,减少
- 本文实例为大家分享了JavaSE实现猜拳游戏的具体代码,供大家参考,具体内容如下import java.util.Random;import
- Android 4.0 系统定义了一系列的高效导航方式 (Effective Navigation), 主要包括标签、下拉列表、以及向上和返
- 前言CyclicBarrier和CountDownLatch这两个工具都是在java.util.concurrent包下,并且平时很多场景都
- 1、mapper.xml文件中的sql语句不提示1.1 首先,alt+enter,出现如下窗口随后的窗口选择这样在如下窗口中会增加一个upd
- 今天写一个小程序中使用到了全局快捷键,找到了我之前写的文章在c#中使用全局快捷键翻了一下,发现它是WinForm版本的,而我现在大部分写WP
- 以最终客户的角度来看,JAR文件就是一种封装,他们不需要知道jar文件中有多少个.class文件,每个文件中的功能与作用,同样可以得到他们希
- 一,内部类访问成员1,内部类可以直接访问外部类的成员,包括私有。2,外部类要访问内部类,必须建立内部类对象。class Outer{int
- 本文实例讲述了java使用Hashtable过滤数组中重复值的方法。分享给大家供大家参考,具体如下:package org.eline.co
- 最近有小伙伴问我mybatis有没有自动创建表结构的功能,因为他们之前一直使用hibernate用习惯了,理所当然的认为,在实体类上配置 *
- redisson的几大特性相信看了这个标题的同学,对这个问题以已经非常不陌生了,信手拈来redisson的几大特性:可重入性【多个业务线同一
- 网络唤醒实现了对网络的集中管理,即在任何时刻,网管中心的IT管理人员可以经由网络远程唤醒一台处于休眠或关机状态的计算机。使用这一功能,IT管
- 前言Google终于发布了一个和SQLite相关的库了。之前一直都是在SQLite、XUtils、greenDao、Realm这些数据库之间
- JDK、JRE、JVMJDK包含JRE,而JRE包含JVMJDK(Java Development Kit)是针对Java开发员的产品,是整
- 本文实例讲述了C#获取字符串后几位数的方法。分享给大家供大家参考。具体实现方法如下:#region 获取后几位数 public string
- 本文实例讲述了Java单例模式。分享给大家供大家参考,具体如下:在实际开发的时候会有一些需求,在某个类中只能允许同时存在一个对象。这时就需要