C#中ArrayList的使用方法

 更新时间:2013年12月09日 15:42:48   作者:  
这篇文章主要介绍了
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; 

相关文章

  • Unity3D生成一段隧道网格的方法

    Unity3D生成一段隧道网格的方法

    这篇文章主要为大家详细介绍了Unity3D生成一段隧道网格的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • C#的十种语法糖介绍

    C#的十种语法糖介绍

    这篇文章介绍了C#的十种语法糖,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • 如何在C#9 中使用static匿名函数

    如何在C#9 中使用static匿名函数

    这篇文章主要介绍了如何在C#9中使用static匿名函数,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • 3种C# 加载Word的方法

    3种C# 加载Word的方法

    本次经验内容分享通过C#程序来加载Word文档的3种不同方法。分别是:加载本地Word文档、以只读模式加载Word文档、从流加载Word 想具体了解的小伙伴请参考下文
    2021-09-09
  • C# Dictionary的使用实例代码

    C# Dictionary的使用实例代码

    C# Dictionary的使用实例代码,需要的朋友可以参考一下
    2013-04-04
  • C#运算符重载用法实例分析

    C#运算符重载用法实例分析

    这篇文章主要介绍了C#运算符重载用法,实例分析了C#中运算符重载的基本实现与使用技巧,需要的朋友可以参考下
    2015-07-07
  • WPF自定义实现雷达图控件的示例详解

    WPF自定义实现雷达图控件的示例详解

    雷达图用于表示不同内容的占比关系,在项目中有广泛的应用,但是目前未曾有封装良好的雷达图控件,所以本文分享了如何封装一个通用的雷达图控件,希望对大家有所帮助
    2023-08-08
  • C#8.0 中开启默认接口实现方法

    C#8.0 中开启默认接口实现方法

    这篇文章主要介绍了C#8.0 中开启默认接口实现方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧的相关资料
    2019-05-05
  • 利用C#实现AOP常见的几种方法详解

    利用C#实现AOP常见的几种方法详解

    AOP面向切面编程(Aspect Oriented Programming),是通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。下面这篇文章主要给大家介绍了关于利用C#实现AOP常见的几种方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-09-09
  • Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作

    Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作

    这篇文章主要介绍了Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作的相关资料,需要的朋友可以参考下
    2015-11-11

最新评论