对象和结构化的数据
集合
作为集合的数组:下面是一个简单的例子:
< %@Page Language="c#" %>
1<script language="c#" runat="server">
2void Page_Load()
3{
4string[] AnimalArray = new string[5]
5{ "Dog", "Cat", "Elephant", "Lion", "Cat"};
6MyLabel.Text = AnimalArray.GetValue(2).ToString()+"<font color=red> "+Array.IndexOf(AnimalArray,"Cat")+"</font>";
7}
8</script>
1<html>
2<asp:label id="MyLabel" runat="server"></asp:label>
3</html>
这个程序输出显然是:Elephant。
下面是一个对数组中某处遍历的程序:
< %@Page Language="c#" %>
1<script language="c#" runat="server">
2void Page_Load()
3{
4int intCounter = -1;
5string[] AnimalArray = new string[5]
6{ "Dog", "Cat", "Elephant", "Lion", "Cat"};
7do
8{
9intCounter = Array.IndexOf(AnimalArray, "Cat", intCounter+1);
10MyText.InnerHtml += "AnimalArray[" + intCounter + "]
11";
12} while (intCounter != Array.LastIndexOf(AnimalArray, "Cat"));
13}
14</script>
1<html>
2The string "Cat" occurs in the following elements:
3
4
5<div id="MyText" runat="server"></div>
6</html>
颠倒数组中元素的顺序:Array.Reverse(Array1); ![]()
将元素排序:Array.Sort(Array1); ![]()
使用数组:
< %@Page Language="c#" %>
1<script language="c#" runat="server">
2void Page_Load()
3{
4string[] AnimalArray = new string[5]
5{ "Dog", "Cat", "Elephant", "Lion", "Cat" };
6Array.Reverse(AnimalArray);
7foreach (string strAnimal in AnimalArray)
8{
9MyDropDownList.Items.Add(strAnimal);
10}
11}
12</script>
1<html>
2<form id="Form1" method="post" runat="server">
3<asp:dropdownlist id="MyDropDownList" runat="server"></asp:dropdownlist>
4</form>
5</html>
数据绑定:
集合的通用功能是:添加一对语句就可以将集合指定为数据源。
如前面的程序:
MyDropDownList.DataSource=AnimalArray;
MyDropDownList.DataBind();
ArrayList
定义:ArrayLins MyArrayList=new ArrayList();
每一个新项都会自动添加到公文的末尾!
前面的程序可以修改成这样:
< %@Page Language="c#" %>
1<script language="c#" runat="server">
2void Page_Load()
3{
4ArrayList AnimalArrayList = new ArrayList();
5AnimalArrayList.Add("Dog");
6AnimalArrayList.Add("Cat");
7AnimalArrayList.Add("Elephant");
8AnimalArrayList.Add("Lion");
9AnimalArrayList.Add("Cat");
10MyDropDownList.DataSource = AnimalArrayList;
11MyDropDownList.DataBind();
12}
13</script>
1<html>
2<form id="Form1" method="post" runat="server">
3<asp:dropdownlist id="MyDropDownList" runat="server"></asp:dropdownlist>
4</form>
5</html>
ArrayList的一些方法:
添加:
MyArrayList.Add(“pig“);
插入:
MyArrayList.Insert(3,“long“);
删除:
MyArrayList.RemoveAt(3);
OR
MyArrayList.Remove(“cat“);
HashTable
创建HashTable:HashTable myHashTable=new HashTable();
添加值的两种方式:
myHashTable.Add([UK],“HongKong“);
OR
myHashTable[UK]=“Hongkong“;
Hashtable.Add() 采用两个参数,一个用于键,一个用于值。两者都属于类型对象。为键传递的值是整数,因此必须将其装箱以便作为对象进行传递。为值传递的值是字符串,它是引用类型,因此不对字符串进行装箱。每答对一个得一分。
e.g.:
< %@Page Language="c#" debug="true" %>
1<script language="c#" runat="server">
2void Page_Load(object source, EventArgs e)
3{
4Hashtable myHashtable = new Hashtable();
5myHashtable["UK"] = "United Kingdom";
6myHashtable["US"] = "United States";
7myHashtable["DE"] = "Germany";
8if (!(Page.IsPostBack))
9{
10foreach (DictionaryEntry Item in myHashtable)
11{
12ListItem newListItem = new ListItem();
13newListItem.Text = Item.Value.ToString();
14newListItem.Value = Item.Key.ToString();
15myDropDownList.Items.Add(newListItem);
16}
17}
18}
19void Click(object source, EventArgs e)
20{
21myLabel.Text = myDropDownList.SelectedItem.Value;
22}
23</script>
1<html>
2<form runat="server">
3<asp:dropdownlist id="myDropDownList" runat="server"></asp:dropdownlist>
4<asp:button id="myButton" onclick="Click" runat="server" text="OK"></asp:button>
5<br/><br/>
6<asp:label id="myLabel" runat="server" text=""></asp:label>
7</form>
8</html>
SortedList
类似HashTable,其中的值排序按照健值排序,而不是值!
使用:
< %@Page Language="c#" debug="true" %>
1<script language="c#" runat="server">
2void Page_Load(object source, EventArgs e)
3{
4SortedList mySortedList = new SortedList();
5mySortedList["armadillo"]="any of a family ... small bony plates";
6mySortedList["amaryllis"]="an autumn-flowering ... Hippeastrum or Sprekelia]";
7mySortedList["zebra"]="any of several fleet ... white or buff";
8mySortedList["artichoke"]="a tall composite herb ... cooked as a vegetable";
9if (!(Page.IsPostBack))
10{
11foreach (DictionaryEntry Item in mySortedList)
12{
13ListItem newListItem = new ListItem();
14newListItem.Text = Item.Key.ToString();
15newListItem.Value = Item.Value.ToString();
16myDropDownList.Items.Add(newListItem);
17}
18}
19}
20void Click(object source, EventArgs e)
21{
22myLabel.Text = myDropDownList.SelectedItem.Value;
23}
24</script>
1<html>
2<form runat="server">
3Pick a word from the list:
4<asp:dropdownlist id="myDropDownList" runat="server"></asp:dropdownlist>
5<asp:button id="myButton" onclick="Click" runat="server" text="OK"></asp:button>
6<br/><br/>
7<b>Definition: </b>
8<asp:label id="myLabel" runat="server" text=""></asp:label>
9</form>
10</html>
(380)