【专用】C# ArrayList的用法总结

devtools/2024/9/23 4:32:04/

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

四、删除

1)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(int index);

移除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中移除所有元素。

五、排序

1)public virtual void Sort();

对ArrayList或它的一部分中的元素进行排序。

ArrayLista List=new ArrayList();

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

2)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");

aList.Add("a");//同0

aList.Add("d");

aList.Add("e");

intnIndex=aList.LastIndexOf("a");//值为2而不是0

g)public virtual bool Contains(object item);

确定某个元素是否在ArrayList中。包含返回true,否则返回false

七、获取数组中的元素

下面以整数为例,给出获取某个元素的值的方法

ArrayList aList=new ArrayList();

for(int i=0;i<10;i++)

     aList.Add(i);

for(i=0;i<10;i++)

    Textbox1.text+=(int)aList[i]+" ";//获取的方式基本与一般的数组相同,使用下标的方式进行

结果为:0 1 2 3 4 5 6 7 8 9

八、其他

1.public virtual int Capacity{get;set;}

获取或设置ArrayList可包含的元素数。

2.public virtual int Count{get;}

获取ArrayList中实际包含的元素数。

Capacity是ArrayList可以存储的元素数。Count是ArrayList中实际包含的元素数。Capacity总是大于或等于Count。如果在添加元素时,Count超过Capacity,则该列表的容量会通过自动重新分配内部数组加倍。

如果Capacity的值显式设置,则内部数组也需要重新分配以容纳指定的容量。如果Capacity被显式设置为0,则公共语言运行库将其设置为默认容量。默认容量为16。

在调用Clear后,Count为0,而此时Capacity却是默认容量16,而不是0

3.public virtual void TrimToSize();

将容量设置为ArrayList中元素的实际数量。

如果不向列表中添加新元素,则此方法可用于最小化列表的内存系统开销。

若要完全清除列表中的所有元素,请在调用TrimToSize之前调用Clear方法。截去空ArrayList会将ArrayList的容量设置为默认容量,而不是零。

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");//Count=5,Capacity=16,

aList.TrimToSize();//Count=Capacity=5;


http://www.ppmy.cn/devtools/42099.html

相关文章

软考--试题六--享元模式(Flyweight)

享元模式(Flyweight) 意图 运用共享技术有效地支持大量细粒度的对象(将对象进行细分) 结构 适用性 1、一个应用程序使用了大量的对象 2、完全由于使用大量的对象&#xff0c;造成很大的存储开销 3、对象的大多数状态都快变为外部状态 4、如果删除对象的外部状态(易变)&…

阅读笔记——《代码整洁之道》ch3

引言 clean-code ch3阅读笔记 短小 函数的第一规则是要短小&#xff0c;一般来说不要一个函数体不要超过半个屏幕。 只做一件事情 函数应该做一件事。做好这件事情。只做一件事。 编写函数毕竟是为了把大一些的概念拆分为另一抽象层上的一系列步骤。只做一件事的函数无法…

在win10折腾Flowise:部署和尝试

Flowise 是一种低代码/无代码拖放工具&#xff0c;旨在让人们轻松可视化和构建 LLM 应用程序。 本地部署 操作系统&#xff1a; win10 由于网络、操作系统等各种未知问题&#xff0c;使用npm install -g flowise的方式&#xff0c;尝试了很多次&#xff0c;都没有部署成功&am…

二.使用PgAdmin连接Postgresql

二.使用PgAdmin连接Postgresql PostgreSQL是一种开源的对象关系型数据库管理系统(ORDBMS),它支持大部分SQL标准并提供了许多高级功能,例如事务、外键、视图、触发器等。PostgreSQL由PostgreSQL全球开发组维护和开发,它是一种高度可扩展的数据库系统,可以在各种操作系统…

专业的服贸会服务团队-媒体邀约宣传

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 2024服贸会开展在即&#xff0c;许多企业都做好了的参展的准备&#xff0c;北京麦塔文化提供专业的展览展会服务&#xff0c;下面做个简单介绍。 、会场搭建团队&#xff1a; 负责整个活…

基于光纤技术的新能源汽车电池安全监测--FOM²系统

为什么要进行动力电池包的温度监测&#xff1f; 新能源电动汽车的动力电池包的工作温度&#xff0c;不仅会影响电池包性能&#xff0c;而且直接关系到车辆安全。时有发生的新能源汽车电池包起火事件&#xff0c;对电池包、冷却系统以及电池管理系统&#xff08;BMS&#xff09…

解决电脑睡眠后,主机ping不通VMware虚拟机

文章目录 问题解决方法方法一方法二注意 问题 原因&#xff1a;电脑休眠一段时间&#xff0c;再次打开电脑就ping不通VMware虚拟机。 解决方法 方法一 重启电脑即可&#xff0c;凡是遇到电脑有毛病&#xff0c;重启能解决90%问题。但是重启电脑比较慢&#xff0c;而且重启…

Ubuntu24安装搜狗输入法,修复闪屏问题

下载deb安装包&#xff1a;搜狗输入法linux-首页 安装&#xff1a;sudo dpkg -i 1.deb 搜狗输入法linux-安装指导 重启&#xff0c;但是完成后闪烁。按以下步骤更改桌面配置。 sudo gedit /etc/gdm3/custom.conf 取消WaylandEnable的注释即可