C# 基础知识之Array和ArrayList用法举例

c347ef3eddf50818fab3e7188e03c86c.png

Array

Array 类是一个抽象类,因此不能实例化一个对象来创建数组

平常我们 int[],string[]... 事实上就是声明一个 Array 数组了

但是可以这样来创建一个数组

Array iArray = new int[] {1,2,3,4,5};

for (int i = 0;i < iArray.Length;i ++)

{

  Console.WriteLine(iArray.GetValue(i));

}

或者使用静态方法 CreateInstance,尤其是事先不知道数组元素类型的时候,可以这样定义数组。其中 SetValue 方法用于设定,GetValue方法用于读取

Array array = Array.CreateInstance(typeof(string),3);

array.SetValue("Tom",0);

array.SetValue("Jack",1);

array.SetValue("Bill",2);

for(int i = 0;i < array.Length;i++)

{

  Console.WriteLine(array.GetValue(i));

}

改方法也可用于创建多维数组。

ArrayList

ArrayList 类是一个特殊的数组,他所属的名称空间是 System.Collections

一、优点

·  支持自动改变大小的功能

·  可以灵活的插入元素

·  可以灵活的删除元素

·  可以灵活的访问元素

二、局限性

跟一般的数组相比,速度上差一些

例子:

ArrayList myArrayList = new ArrayList();

myArrayList.Add(1);

myArrayList.Add(2);

myArrayList.Add(3);

或者可以直接添加一个数组给 ArrayList

int[] myInt = {1,2,3};

myArrayList.AddRange(myInt);

下面详细介绍 ArrayList 的添删改等操作

三、添加元素

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

五、排序

1.  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;//DropDownListDropDownList1;

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

六、查找

1.  public virtual int IndexOf(object);

2.  public virtual int IndexOf(object,int);

3.  public virtual intIndexOf(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

4.  public virtual int LastIndexOf(object);

5.  public virtual intLastIndexOf(object,int);

6.  public virtual intLastIndexOf(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

7.  public virtual bool Contains(objectitem);

确定某个元素是否在 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 45 6 7 8 9

八、其他

1.public virtual intCapacity{get;set;}

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

2.public virtual intCount{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;

往期推荐

3cb60c07e95b18ba99c61e113e001f3b.png

Love life,love yourself

关注小编不迷路呦~

欢迎关注公众号: dotnet编程大全

技术群: 需要进技术群的添加小编微信mm1552923,备注:加群;

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值