ArrayList
System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度.
一.优点
1.支持自动改变大小的功能
2.可以灵活的插入元素
3.可以灵活的删除元素
二.局限性
跟一般的数组比起来,速度上差些
方法:
public virtual int Add( object value ); 添加元素
public virtual void Insert( int index,object value );
将元素插入 ArrayList 的指定索引处
public virtual void InsertRange( int index,ICollection c );
将集合中的某个元素插入 ArrayList 的指定索引处
public virtual void Remove( object obj );
从 ArrayList 中移除特定对象的第一个匹配项,注意是第一个
public virtual void RemoveAt( int index );
移除 ArrayList 的指定索引处的元素
public virtual void RemoveRange( int index,int count );
从 ArrayList 中移除一定范围的元素.
Index表示索引,count表示从索引处开始的数目
public virtual void Clear( );
从 ArrayList 中移除所有元素.
public virtual void Sort( );
对 ArrayList 或它的一部分中的元素进行排序.
public virtual void Reverse( );
将 ArrayList 或它的一部分中元素的顺序反转.
查找
a ) public virtual int IndexOf( object );
b ) public virtual int IndexOf( object, int );
c ) public virtual int IndexOf( object, int, int );
获取或设置 ArrayList 可包含的元素数.
2.public virtual int Count
{
get;
}
.net 列子:
studyArraylist.aspx
<%@ Page language="c#" Codebehind="StudyOfArrayList.aspx.cs" AutoEventWireup="false" Inherits="MySample.Web.StudyOfArrayList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>StudyOfArrayList</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"></FONT>
<p>ArrayList(数组)</p>
<table width="86%" border="1">
<tr>
<td height="162">ArrayList<br>
System.Collections.ArrayList类是一个特殊的数组.通过添加和删除元素,就可以动态改变数组的长度.<br>
一.优点<br>
1.支持自动改变大小的功能<br>
2.可以灵活的插入元素<br>
3.可以灵活的删除元素<br>
二.局限性<br>
跟一般的数组比起来,速度上差些
</td>
</tr>
<tr>
<td height="660">
<p>方法:</p>
<p>public virtual int Add( object value ); 添加元素</p>
<p>public virtual void Insert( int index,object value );<br>
将元素插入 ArrayList 的指定索引处</p>
<p>public virtual void InsertRange( int index,ICollection c );<br>
将集合中的某个元素插入 ArrayList 的指定索引处</p>
<p>public virtual void Remove( object obj );<br>
从 ArrayList 中移除特定对象的第一个匹配项,注意是第一个</p>
<p>public virtual void RemoveAt( int index );<br>
移除 ArrayList 的指定索引处的元素</p>
<p>public virtual void RemoveRange( int index,int count );<br>
从 ArrayList 中移除一定范围的元素.<br>
Index表示索引,count表示从索引处开始的数目</p>
<p>public virtual void Clear( );<br>
从 ArrayList 中移除所有元素.</p>
<p>public virtual void Sort( );<br>
对 ArrayList 或它的一部分中的元素进行排序.</p>
<p>public virtual void Reverse( );<br>
将 ArrayList 或它的一部分中元素的顺序反转.<br>
</p>
<p>查找<br>
a ) public virtual int IndexOf( object );<br>
b ) public virtual int IndexOf( object, int );<br>
c ) public virtual int IndexOf( object, int, int );</p>
<p>获取或设置 ArrayList 可包含的元素数.<br>
2.public virtual int Count
<br>
{<br>
get;<br>
}<br>
</p>
</td>
</tr>
<tr>
<td>
<p>具体演示: 显示数据:
<asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist></p>
<p>添加:
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:button id="Button1" runat="server" Text="添加"></asp:button></p>
<p>在指定位置添加 位置:
<asp:dropdownlist id="DropDownList2" runat="server"></asp:dropdownlist><asp:textbox id="TextBox2" runat="server"></asp:textbox><asp:button id="Button2" runat="server" Text="添加"></asp:button></p>
<p>在指定位置添加数组:
<asp:dropdownlist id="DropDownList3" runat="server"></asp:dropdownlist><asp:button id="Button3" runat="server" Text="添加"></asp:button></p>
<p>删除:
<asp:textbox id="TextBox3" runat="server"></asp:textbox><asp:button id="Button7" runat="server" Text="删除"></asp:button><asp:label id="Label2" runat="server">Label</asp:label></p>
<p>排序:
<asp:button id="Button4" runat="server" Text="顺序"></asp:button>
<asp:button id="Button5" runat="server" Text="倒序"></asp:button></p>
<p>查找:
<asp:textbox id="TextBox4" runat="server" Width="72px"></asp:textbox><asp:button id="Button6" runat="server" Text="查"></asp:button><asp:label id="Label1" runat="server" Width="40px">Label</asp:label></p>
</td>
</tr>
<tr>
<td>
<p>数据绑定:</p>
<p><FONT face="宋体"></FONT> </p>
</td>
</tr>
</table>
</form>
</body>
</HTML>
StudyArraylist.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace MySample.Web
{
/// <summary>
/// StudyOfArrayList 的摘要说明。
/// </summary>
public class StudyOfArrayList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.Button Button4;
protected System.Web.UI.WebControls.Button Button5;
protected System.Web.UI.WebControls.TextBox TextBox4;
protected System.Web.UI.WebControls.Button Button6;
protected System.Web.UI.WebControls.Label Label1;
public ArrayList arrl=new ArrayList();
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button7;
protected System.Web.UI.WebControls.Label Label2;
public ArrayList arrl2=new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
arrl.Add("北京");
arrl.Add("上海");
arrl.Add("天津");
arrl.Add("广州");
arrl2.Add("北京2");
arrl2.Add("上海2");
arrl2.Add("天津2");
arrl2.Add("广州2");
bindData();
}
if (Cache["arrl"]==null)
Cache.Insert("arrl",(ArrayList)arrl);
else
arrl=(ArrayList)Cache["arrl"];
// 在此处放置用户代码以初始化页面
}
private void bindData()
{
if (Cache["arrl"]!=null)
arrl=(ArrayList)Cache["arrl"];
DropDownList1.DataSource=arrl;
DropDownList1.DataBind();
DropDownList2.DataSource=arrl;
DropDownList2.DataBind();
DropDownList3.DataSource=arrl;
DropDownList3.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.Button5.Click += new System.EventHandler(this.Button5_Click);
this.Button6.Click += new System.EventHandler(this.Button6_Click);
this.Button7.Click += new System.EventHandler(this.Button7_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
if (TextBox1.Text!="")
arrl.Add(TextBox1.Text);
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button2_Click(object sender, System.EventArgs e)
{
if (TextBox2.Text!="")
{
arrl.Insert(arrl.IndexOf(DropDownList2.SelectedValue),TextBox2.Text);
}
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button3_Click(object sender, System.EventArgs e)
{
arrl.InsertRange(arrl.IndexOf(DropDownList2.SelectedValue),arrl2);
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button7_Click(object sender, System.EventArgs e)
{
if (TextBox3.Text!="")
{
if (arrl.IndexOf(TextBox3.Text)==-1)
{
Label2.Text="未找到指定内容";
}
else
{
arrl.Remove(TextBox3.Text);
Label2.Text="删除成功";
}
}
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button4_Click(object sender, System.EventArgs e)
{
arrl.Sort();
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button5_Click(object sender, System.EventArgs e)
{
arrl.Reverse();
Cache.Insert("arrl",(ArrayList)arrl);
bindData();
}
private void Button6_Click(object sender, System.EventArgs e)
{
if (TextBox4.Text!="")
{
if (arrl.IndexOf(TextBox4.Text)==-1)
{
Label1.Text="未找到指定内容";
}
else
{
Label1.Text="找到内容";
}
}
}
}
}