C#6-数组和集合对象_第1页
C#6-数组和集合对象_第2页
C#6-数组和集合对象_第3页
C#6-数组和集合对象_第4页
C#6-数组和集合对象_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六节数组和集合对象目标数组的使用使用System.Array对象理解集合对象的特点和优点使用System.ArrayList对象使用哈希表对象数组

数组是一种包含若干变量的数据结构,这些变量都可以通过计算索引进行访问。数组中的数组的元素具有相同的类型。数组有一个“秩”。数组的秩又称为数组的维度。“秩”为

1

的数组称为一维数组。“秩”大于

1

的数组称为多维数组。维度大小确定的多维数组通常称为两维数组、三维数组等。

声明数组

声明数组时,方括号

([])

必须跟在类型后面,而不是标识符后面。在

C#

中,将方括号放在标识符后是不合法的语法。C#

支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。

一维数组:

int[]

arrayname;

多维数组:

int[,]

arrayname;

数组的数组(交错的):

int[][]

arrayname

;

注意:

声明数组并不实际创建它们。在

C#

中,数组是对象,必须进行实例化。

数组示例using

System;

class

TestArray{

public

static

void

Main()

{

//声明一个整型一维数组的引用,变且在堆中分配连续5个整型变量的空间。

int[]

numbers

=

new

int[5];

//

声明一个二维字符串数组的引用

string[,]

names

=

new

string[5,4];

//

数组的数组,相当声明了包含5个byte型一维数组的引用变量的一维数组长度为5

byte[][]

scores

=

new

byte[5][];

//为每个btye型一维数组实例化

for

(int

i

=

0;

i

<

scores.Length;

i++)

{

scores[i]

=

new

byte[i+3];

//非矩形的

}

for

(int

i

=

0;

i

<

scores.Length;

i++)

{

Console.WriteLine("Length

of

row

{0}

is

{1}",

i,

scores[i].Length);

}

}

}

初始化数组C#

通过将初始值括在大括号

({})

内为在声明时初始化数组提供了简单而直接了当的方法。一维数组

int[]

numbers

=

new

int[5]

{1,

2,

3,

4,

5};

string[]

names

=

new

string[3]

{"Matt",

"Joanne",

"Robert"};

可省略数组的大小

int[]

numbers

=

new

int[]

{1,

2,

3,

4,

5};

string[]

names

=

new

string[]

{"Matt",

"Joanne",

"Robert"};

如果提供了初始值设定项,则还可以省略

new

运算符

int[]

numbers

=

{1,

2,

3,

4,

5};

string[]

names

=

{"Matt",

"Joanne",

"Robert"};初始化数组C#

通过将初始值括在大括号

({})

内为在声明时初始化数组提供了简单而直接了当的方法。多维数组

int[,]

numbers

=

new

int[3,

2]

{

{1,

2},

{3,

4},

{5,

6}

};

string[,]

siblings

=

new

string[2,

2]

{

{"Mike","Amy"},

{"Mary","Albert"}

};

可省略数组的大小

int[,]

numbers

=

new

int[,]

{

{1,

2},

{3,

4},

{5,

6}

};

string[,]

siblings

=

new

string[,]

{

{"Mike","Amy"},

{"Mary","Albert"}

};

如果提供了初始值设定项,则还可以省略

new

运算符int[,]

numbers

=

{

{1,

2},

{3,

4},

{5,

6}

};

string[,]

siblings

=

{

{"Mike",

"Amy"},

{"Mary",

"Albert"}

};初始化数组C#

通过将初始值括在大括号

({})

内为在声明时初始化数组提供了简单而直接了当的方法。交错的数组(数组的数组)

int[][]

numbers

=

new

int[2][]

{

new

int[]

{2,3,4},

new

int[]

{5,6,7,8,9}

};

可省略第一个数组的大小int[][]

numbers

=

new

int[][]

{

new

int[]

{2,3,4},

new

int[]

{5,6,7,8,9}

};

-或-

int[][]

numbers

=

{

new

int[]

{2,3,4},

new

int[]

{5,6,7,8,9}

};

访问数组成员访问数组成员可以直接进行,类似于在

C/C++

中访问数组成员。下面的代码创建一个名为

numbers

的数组,然后向该数组的第五个元素赋以

5:

int[]

numbers

=

{10,

9,

8,

7,

6,

5,

4,

3,

2,

1,

0};

numbers[4]

=

5;

下面的代码声明一个多维数组,并向位于

[1,

1]

的成员赋以

5:

int[,]

numbers

=

{

{1,

2},

{3,

4},

{5,

6},

{7,

8},

{9,

10}

};

numbers[1,

1]

=

5;

下面声明一个一维交错数组,它包含两个元素。第一个元素是两个整数的数组,第二个元素是三个整数的数组:

int[][]

numbers

=

new

int[][]

{

new

int[]

{1,

2},

new

int[]

{3,

4,

5}};

下面的语句向第一个数组的第一个元素赋以

58,向第二个数组的第二个元素赋以

667:

numbers[0][0]

=

58;

numbers[1][1]

=

667;

对数组使用

foreachC#

还提供

foreach

语句。该语句提供一种简单、明了的方法来循环访问数组的元素。int[]

numbers

=

{4,

5,

6,

1,

2,

3,

-2,

-1,

0};

foreach

(int

i

in

numbers)

{

System.Console.WriteLine(i);

}

对数组使用

foreachC#

还提供

foreach

语句。该语句提供一种简单、明了的方法来循环访问数组的元素。int[,]

numbers

=

new

int[3,

2]

{{9,

99},

{3,

33},

{5,

55}};

foreach(int

i

in

numbers)

{

Console.Write("{0}

",

i);

}

System.Array类

C#

中,数组实际上是对象。System.Array

是所有数组类型的抽象基类型。System.Array

提供创建、操作、搜索和排序数组的方法,因而在公共语言运行库中用作所有数组的基类。所有数组都可以使用System.Array的属性和方法。

常用属性和方法

Length属性

表示数组所有维数中元素的总数。

int

[]

number={1,2,3,4};

number.Length的值为4;

Rank属性

表示数组中的维数。string[,]

names

=

new

string[5,4];

names.Rank的值为2。

常用属性和方法

Sort方法

对一维数组排序。它是Array类的静态方法.

string

[]

name=new

string[]{"xi","ang","zhang","chun"};

Array.Sort(name);

foreach(string

s

in

name)

{

Console.WriteLine(s);

}

Reverse方法

反转一维数组

Array.Reverse(name);

其值为:chun,zhang,ang,xi

常用属性和方法

GetLowerBound与GetUpperBound方法

数组指定维度的下限与上限

int

[,,]

number=new

int[4,3,2]{{{1,2},{2,3},{3,4}},{{4,5},{5,6},{6,7}},{{7,8},{8,9},{9,10}},{{10,11},{11,12},{12,13}}};

for(int

i=number.GetLowerBound(0);i<=number.GetUpperBound(0);i++)

{

for(int

j=number.GetLowerBound(1);j<=number.GetUpperBound(1);j++)

{

for(int

k=number.GetLowerBound(2);k<=number.GetUpperBound(2);k++)

{

Console.WriteLine(“number[{0},{1},{2}]={3}”,i,j,k,number[i,j,k]);

}

}

}

Clear方法

重新初始化数组中所有的元素

将数组中的一系列元素设置为零、false

或空引用.

常用属性和方法CreateInstance方法创建数组

Arrayobj=Array.CreateInstance(typeof(string),10);

staticvoidMain(string[]args){

//构建objNames数组

ArrayobjNames=Array.CreateInstance(typeof(string),5);

//初始化值

objNames.SetValue(“A",0);objNames.SetValue(“B",1);objNames.SetValue(“C",2);objNames.SetValue(“D",3);objNames.SetValue(“E",4);Console.WriteLine(“数组值");

for(intctr=0;ctr<5;ctr++){

Console.WriteLine(“元素{0}:{1}",ctr+1,objNames.GetValue(ctr));

}}

System.Collections简介System.Collections命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。

哈希表(Hashtable)简述Hashtable是System.Collections命名空间提供的一个容器用于处理和表现类似key/value的键值对key通常可用来快速查找,同时key是区分大小写;value用于存储对应于key的值。Hashtable中key/value键值对均为object类型,所以Hashtable可以支持任何类型的key/value键值对.

哈希表的简单操作在哈希表中添加一个key/value键值对: HashtableObject.Add(key,value);

在哈希表中去除某个key/value键值对: HashtableObject.Remove(key);

从哈希表中移除所有元素:

HashtableObject.Clear();

判断哈希表是否包含特定键key:

HashtableObject.Contains(key);

usingSystem;

usingSystem.Collections;//使用Hashtable时,必须引入这个命名空间

classhashtable

{

publicstaticvoidMain()

{

Hashtableht=newHashtable();//创建一个Hashtable实例

ht.Add(“E”,“e”);//添加key/value键值对

ht.Add(“A”,“a”);

ht.Add(“C”,“c”);

ht.Add(“B”,“b”);

strings=(string)ht[“A”];

if(ht.Contains(“E”))//判断哈希表是否包含特定键,其返回值为true或false

Console.WriteLine(“theEkey:exist”);

ht.Remove(“C”);//移除一个key/value键值对

Console.WriteLine(ht[“A”]);//此处输出a

ht.Clear();//移除所有元素

Console.WriteLine(ht["A"]);//此处将不会有任何输出

}

}

遍历哈希表

遍历哈希表需要用到DictionaryEntryObject:

for(DictionaryEntrydeinht)//ht为一个Hashtable实例

{

Console.WriteLine(de.Key);//de.Key对应于key/value键值对key

Console.WriteLine(de.Value);//de.Key对应于key/value键值对value

}对哈希表进行排序对哈希表进行排序在这里的定义是对key/value键值对中的key按一定规则重新排列但是实际上这个定义是不能实现的,因为我们无法直接在Hashtable进行对key进行重新排列如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:

ArrayListakeys=newArrayList(ht.Keys);//别忘了导入System.Collections

akeys.Sort();//按字母顺序进行排序

for(stringskeyinakeys)

{

Console.Write(skey+":");

Console.WriteLine(ht[skey]);//排序后输出

}

ArrayList类Array类的容量或元素数是固定的,而ArrayList类的容量可以根据需要动态扩展。通过设置ArrayList.Capacity的值可以重新分配内存和复制元素使用ArrayList提供的方法可以同时添加、插入或移除一个范围内的元素

优点

支持自动改变大小的功能

可以灵活的插入元素

可以灵活的删除元素

局限性

跟一般的数组比起来,速度上差些添加元素

将对象添加到ArrayList的结尾处

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

内容为:abcde添加元素

将元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Insert(0,"aa");

结果为:aaabcde添加元素

将集合中的某个元素插入ArrayList的指定索引处

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

ArrayListlist2=newArrayList();

list2.Add("tt");

list2.Add("ttt");

aList.InsertRange(2,list2);

结果为:abtttttcde删除

从ArrayList中移除特定对象的第一个匹配项,注意是第一个

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Remove("a");

结果为:bcde

删除

移除ArrayList的指定索引处的元素

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveAt(0);

结果为:bcde删除

从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveRange(1,3);

结果为:ae删除

从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveRange(1,3);

结果为:ae从ArrayList中移除所有元素aList.Clear();排序

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

ArrayListaList=newArrayList();

aList.Add("e");

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

DropDownList1.DataSource=aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

结果为:eabcdaList.Sort();//排序

DropDownList2.DataSource=aList;//DropDownListDropDownList2;

DropDownList2.DataBind();

结果为:abcde反转

将ArrayList或它的一部分中元素的顺序反转。

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Reverse();//反转

DropDownList1.DataSource=aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

结果为:edcba查找

返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

intnIndex=aList.IndexOf(“a”);//0

nIndex=aList.IndexOf(“p”);//没找到,-1查找

返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。

ArrayListaList=newArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("a");//同0

aList.Add("d");

aList.Add("e")

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论