网站首页站长博客下载中心域名交易站长论坛域名主机免费电邮免费域名中文排行排名查询站长书库书籍教程下载
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
学院首页 网络编程 网页设计 图形图象 数 据 库 服 务 器 网络媒体 网络安全 个人专栏 站长CLUB 业界新闻 信息公告
 当前位置:首页 >> 网络编程 >> NET专区 >> 正文
公告通知
返回上级列表
资料搜索
相关文章
用户自定义控件的应用
c#.net常用函数和方法集
在VB中使用水晶报表的一种简易编
C#调用父类的父类的方法
浏览.NET Framework 2.0 类型库中
为.Text Blog 添加 计数器
编程实现邮件地址有效性检测 
VB/VB.NET/C#导出到Excel的方法
c#高性能在WEB端产生验证图片
用System.Web.Caching.Cache保存
ArrayList-asp.net入门笔记
[ 来源:CSDN | 作者:未知 | 时间:2006-4-2 10:00:56 | 浏览:人次 ]
收藏到新浪ViVi 收藏到365KEY 收藏到我摘  字号选择〖    〗/ 双击滚屏 单击停止  
 

ArrayList

  System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。

一.优点

1。支持自动改变大小的功能

2。可以灵活的插入元素

3。可以灵活的删除元素

二.局限性

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

 

三.添加元素

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

内容为a b c d e

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

结果为aa a b  c d e

3.  public virtual void InsertRange(int index,ICollection c);

   将集合中的某个元素插入 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);

结果为a b tt ttt c d e

四.删除

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

结果为b c d e

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

结果为b c d e

 

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

结果为a e

4.  public virtual void Clear();

ArrayList 中移除所有元素。

 

五.排序

a)       public virtual void Sort();

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

ArrayList aList = new ArrayList();

aList.Add("e");

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

DropDownList1.DataSource = aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为e a b c d

ArrayList aList = new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Sort();  //排序

DropDownList1.DataSource = aList;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为a b c d e

b)       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;  // DropDownList DropDownList1;

DropDownList1.DataBind();

结果为 e d c b a

六.查找

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

int nIndex = 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");

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

g)       public virtual bool Contains(object item);

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

七.其他

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://edu.chinaz.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
栏目编辑: 设计风 责任编辑: keke
原始作者: 未知 录入时间: 2006-4-2 10:00:56
信息来源: CSDN 投稿信箱: Edu#chinaz.com
设为首页 - 加入收藏 - 关于我们 - 广告服务 - 版权申明 - 友情链接 - 联系方式 - 总编信箱 - 会员投稿