实验八集合泛型使用.docx

上传人:b****8 文档编号:10089733 上传时间:2023-02-08 格式:DOCX 页数:14 大小:35.74KB
下载 相关 举报
实验八集合泛型使用.docx_第1页
第1页 / 共14页
实验八集合泛型使用.docx_第2页
第2页 / 共14页
实验八集合泛型使用.docx_第3页
第3页 / 共14页
实验八集合泛型使用.docx_第4页
第4页 / 共14页
实验八集合泛型使用.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

实验八集合泛型使用.docx

《实验八集合泛型使用.docx》由会员分享,可在线阅读,更多相关《实验八集合泛型使用.docx(14页珍藏版)》请在冰豆网上搜索。

实验八集合泛型使用.docx

实验八集合泛型使用

实验八集合、泛型使用

实验报告八

实验课程面向对象程序设计集合、泛型使用成绩项目

实验6/6学号姓名赵旭东201531060753日期

专业指导信息管理与信息系统15级2班杨力班级教师一【实验目的】

1、初步掌握常用集合的创建和操作方法。

2、初步掌握索引器的定义域使用。

3、初步掌握泛型接口、泛型类、泛型属性和泛型方法的使用。

二【实验内容】

使用集合类需要加上名称空间

usingSystem.Collection

1、arraylist类的使用动态数组使用方便,仔细阅读下面的代码了解arraylist的使用

(1)创建动态数组

ArrayListaList=newArrayList();//大小不固定,根据增加和删除元素自动扩展

(2)数组增加元素

aList.Add(3);//

aList.Add(stu1);//元素使对象

(3)取得动态数组实际元素个数count属性

for(inti=0;i

Console.WriteLine(aList[i]);//输出每个元素aList[i]表示数组中第i的元素的值

Studentst1

st1=(Student)aList[0];//如果数组中存放对象需要进行强制转换

(4)insert方法在指定位置插入元素

aList.Insert(0,5);//在下标为0的位置插入5后面的元素自动后移

(5)Remove(objectobj);从ArrayList中移除特定对象的第一个匹配项,注意是第一个

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("c");

aList.Remove("c");

for(inti=0;i

Console.Write(aList[i]);

输出abdc

(6)RemoveAt(intindex);移除ArrayList的指定索引处的元素

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveAt(0);//删除下标0的元素,后面元素依次向前移动

结果为bcde

(7)Sort();对ArrayList或它的一部分中的元素进行排序。

ArrayListaList=newArrayList();

for(inti=9;i>0;i--)

aList.Add(i);

aList.Sort();//从小到大排序

for(inti=0;i

Console.WriteLine(aList[i]);

利用以上介绍的方法编程实现:

定义一个teacher类,包含姓名和职称和一个输出自己信息的方法,建立arraylist类动态数组将3

个teacher类对象的增加到数组中,在索引值为1的位置插入一个teacher类对象,删除索引值

为1的元素,最后输出数组中所有元素的信息

运行结果如下:

代码如下:

usingSystem;

usingSystem.Collections.Generic;usingSystem.Collections;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;

namespaceConsoleApplication1{

classteacher{

stringname,zhicheng;

publicteacher()

{

Console.WriteLine("*********************************");

}

publicteacher(stringname,stringzhicheng){

this.name=name;this.zhicheng=zhicheng;

}

publicvoidoutput(teacherp){

Console.WriteLine("该老师名字:

{0}"+"该老师的职称为:

{1}",p.name,p.zhicheng);

}

}

classProgram

{

staticvoidMain(string[]args)

{

ArrayListaList=newArrayList(10);

teachera=newteacher("于建","高级教师");

teacherb=newteacher("蔡天菊","特级教师");

teacherc=newteacher("王小勇","终极教师");

teacherd=newteacher("赵旭东","低级教师");

aList.Add(a);

aList.Add(b);

aList.Add(c);

aList.Insert(1,d);

aList.RemoveAt

(1);

for(inti=0;i

{

teacherx=(teacher)aList[i];

x.output(x);

Console.WriteLine("******************************");

}

Console.Read();

}

}

}

可参看教材154-157页

2.索引器

类中保护数组对象时,可通过索引器实现方便访问

索引器用来访问类中的数组型对象元素

定义索引器与定义属性类似,一般形式为:

[修饰符]数据类型this[intindex]{

get//获取数组成员指定索引的值

{

returnScoreArray[index];

}

set//利用外部传递的value值给数组赋值

{

ScoreArray[index]=value;

}

}

观察下面的程序是如何访问数组成员的

usingSystem;

usingSystem.IO;

classIndexerClass

{

publicint[]ScoreArray=newint[10];//定义公有数组成员

publicvoidoutput()

{

foreach(intvinScoreArray)

Console.Write("{0}",v);

}

}

publicclassMainClass

{

publicstaticvoidMain()

{

IndexerClassb=newIndexerClass();

b.ScoreArray[0]=1;//使用公用成员数组给数组对应索引赋值

b.ScoreArray[1]=2;

Console.WriteLine("{0}",b.ScoreArray[1]);//使用公用数组成员获取对于索引的数组值

Console.WriteLine("{0}",b.ScoreArray[0]);

b.output();

Console.Read();

}

}

改写上面的程序,仔细观察索引器的实现,并掌握每句代码的意义并允许观察结果usingSystem;

classIndexerClass

{

privateint[]ScoreArray=newint[10];//可定义为私有数组成员

publicintthis[intindex]//索引器的声明通过索引器访问具体索引的数组值

{

get

{

//检查索引器的范围

if(index<0||index>=10)

return0;

else

returnScoreArray[index];

}

set

{

if(!

(index<0||index>=10))

ScoreArray[index]=value;

}

}

}

publicclassMainClass

{

publicstaticvoidMain()

{

IndexerClassb=newIndexerClass();

//调用索引器初始化数组中的第4个和第6个元素

b[3]=256;//通过数组下标来给对象中对应数组赋值

b[5]=1024;

for(inti=0;i<=11;i++)

{

Console.WriteLine("数组元素#{0}={1}",i,b[i]);//通过下标获取对象中数组成员的只

}

Console.Read();

}

}

仿照以上例子,利用索引器实现下面程序

编程:

定义一个Student类,包含学号和姓名两个字段,并定义一个班级类ClassList,包含一个具有5个

元素Student数组对象,利用索引器访问该数组对象,并在主函数中访问。

运行结果如图:

代码如下:

usingSystem;

usingSystem.Collections;

classclasslist

{

publicstudent[]ScoreArray=newstudent[5];

//可定义为私有数组成员

publicstudentthis[intindex]//索引器的声明通过索引器访问具体索引的数组值

{

get

{

//检查索引器的范围

returnScoreArray[index];

}

set

{

if(!

(index<0||index>=5))

ScoreArray[index]=value;

}

}

}

classstudent{

stringname;

intxuehao;

publicstudent(stringname,intxuehao){

this.name=name;

this.xuehao=xuehao;

}

publicvoidoutput(){

Console.WriteLine("名字:

{0}"+"学号:

{1}",name,xuehao);

}

}

publicclassMainClass

{

publicstaticvoidMain()

{

classlistb=newclasslist();

//调用索引器初始化数组中的第4个和第6个元素

studentA=newstudent("赵旭东",201531);

studentB=newstudent("张1",201579);

studentC=newstudent("张2",201571);

studentD=newstudent("张三",201572);

studentE=newstudent("张4",201573);

b[0]=A;

b[1]=B;

b[2]=C;

b[3]=D;

b[4]=E;

for(inti=0;i<=11;i++)

{

studentx=b[i];

x.output();//通过下标获取对象中数组成员的只

}

Console.Read();

}

}

3.泛型

一般我们定义类或函数,其数据成员都指定数据类型,但是为了程序的重用性,我们可以不指明

具体数据类型而是用一个符号来表示,在程序执行的时候将这个符号替换为具体数据类型,这

就是泛型的使用

观察下面的程序读懂每句代码的意义

usingSystem;

usingSystem.IO;

classStudent

{

intno;

stringname;

publicStudent(intno1,stringname1)

{

no=no1;

name=name1;

}

publicstringGet_msg()

{

returnno.ToString()+":

"+name;

}

}

publicclassMainClass

{

publicstaticvoidMain()

{

Studentb=newStudent(1001,"令?

?

狐?

?

冲?

");

Console.WriteLine("{0}",b.Get_msg());

Console.Read();

}

}

我们将上面的代码中数据类型进行符号化,就变为泛型,观察下面的程序usingSystem;

usingSystem.IO;

classStudent//指定数据类型参数T1,T2可分别代表不同数据类型这是泛型类{

T1no;

T2name;

publicStudent(T1no1,T2name1)

{

no=no1;

name=name1;

}

publicvoidSet_msg(T1no1,T2name1)//形参可以是参数化类型这是泛型方法

{

no=no1;

name=name1;

}

publicstringGet_msg()

{

returnno.ToString()+":

"+name;

}

}

publicclassMainClass

{

publicstaticvoidMain()

{

Studentb=newStudent(1001,"令狐冲");//注意创建对象的格式//数据成员1个是int对于类中的T1另1个是string对于类中的T2

Console.WriteLine("{0}",b.Get_msg());

Studentb1=newStudent("1001","令狐冲");////数据成员1个是string对于类中的T1另1个是string对于类中的T2

b1.Set_msg("1003","张三");//直接传递对于数据类型的常量或变量指定具体数据类型

//“1003”是string类型对于Set_msg函数的第一个参数数据类型为string,第2个参数类型也为string

Console.WriteLine("{0}",b1.Get_msg());

Console.WriteLine("{0}",b.Get_msg());

Console.Read();

}

}

编程:

仿照以上例子实现:

定义一个泛型类Data,对10个元素进行排序,求和,求最大和最小值,并通过主函数进行验证。

可参考教材173classData部分

.课后熟悉教程List和Dictionary系统预定义泛型类的使用

定义一个teacher类,包含姓名和职称和一个输出自己信息的方法,用List创建动态数组

将3个teacher类对象的增加到数组中,在索引值为1的位置插入一个teacher类对象,删除索

引值为1的元素,最后输出数组中所有元素的信息(List类是arraylist的泛型类,也就是可以

指定数组元素的数据类型,使用方法与arraylist类类似)

实验报告要求

将以上各题的源程序、运行结果写在该题后面,以及实验中遇到的问题和解决问题的方法,以及实验过程中的心得体会,写在下面的空白中。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1