ASP.NET之ArrayList简介
ASP.NET Web Forms - ArrayList 对象
ArrayList 对象是包含单个数据值的项目的集合。
System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。
一.优点
支持自动改变大小的功能
可以灵活的插入元素
可以灵活的删除元素
二.局限性
跟一般的数组比起来,速度上差些
绑定数据到 ArrayList
ArrayList 对象可为下列的控件自动生成文本和值:
asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html>
然后添加创建列表的脚本,并且绑定列表中的值到 RadioButtonList 控件:
注释:数据值作为控件的 Text 和 Value 属性来使用。如需添加不一样于 Text 的 Value,请使用 Hashtable 对象或者 SortedList 对象。
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");
内容为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
原文链接: https://www.yukx.com/yukx/article/details/582.html 优科学习网ASP.NET之ArrayList简介
-
项目中,有些函数需要处理某个服务的返回结果,而在对函数单元测试的时候,又不能启动那些服务,这里就可以利用Mockito工具,其中有如下三种注解:@InjectMocks:创建一个实例,简单的说是这个Mock可以调用真实代码的方法,其余用@Mock(或@Spy)注解创建的mock将被注入到用该实例中。
-
雪花算法(Snowflake)是由Twitter开发的一种分布式ID生成算法,旨在为分布式系统提供一种简单而有效的方式,以生成全局唯一、有序且可排序的64位整数ID。这种ID通常用作数据库记录的主键或其他需要唯一标识符的场景。雪花算法生成的64位ID结构如下:最高位(第64位):固定为0,因为64位
-
在HTML中,如果你想让一个输入框(input元素)不可编辑,你可以通过设置其readonly属性来实现。示例如下:input type="text" value="此处内容不可编辑" readonly在上述代码中,readonly属性使得用户无法修改输入框中的内容。另外,如果你希望输入框完全不可交
-
ASP.NET教程ASP.NET又称为ASP+,基于.NETFramework的Web开发平台,是微软公司推出的新一代脚本语言。ASP.NET是一个使用HTML、CSS、JavaScript和服务器脚本创建网页和网站的开发框架。ASP.NET支持三种不一样的开发模式:WebPages(Web页面)、
-
C# 判断判断结构要求程序员指定一个或多个要评估或测试的条件,以及条件为真时要执行的语句(必需的)和条件为假时要执行的语句(可选的)。下面是大多数编程语言中典型的判断结构的通常形式:判断语句C#提供了以下类型的判断语句。点击链接查看每个语句的细节。语句描述if语句一个 if语句 由一个布尔表达式后跟
-
C#循环有的时候,可能需要多次执行同一块代码。通常情况下,语句是顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推。编程语言提供了允许更为复杂的执行路径的多种控制结构。循环语句允许我们多次执行一个语句或语句组,下面是大多数编程语言中循环语句的通常形式:循环类型C#提供了以下几种循环类型