ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:35.74KB ,
资源ID:10089733      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10089733.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验八集合泛型使用.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验八集合泛型使用.docx

1、实验八集合泛型使用实验八 集合、泛型使用实 验 报 告 八 实验 课 程 面向对象程序设计 集合、泛型使用 成 绩 项目 实验 6/6 学 号 姓 名 赵旭东 201531060753 日期 专业 指导 信息管理与信息系统15级2班 杨 力 班级 教师 一【实验目的】 1、初步掌握常用集合的创建和操作方法。 2、初步掌握索引器的定义域使用。 3、初步掌握泛型接口、泛型类、泛型属性和泛型方法的使用。 二【实验内容】 使用集合类 需要加上名称空间 using System.Collection 1、 arraylist类的使用 动态数组 使用方便,仔细阅读下面的代码 了解arraylist的使用

2、(1)创建动态数组 ArrayList aList=new ArrayList(); /大小不固定,根据增加和删除元素自动扩展 (2)数组增加元素 aList.Add(3); / aList.Add(stu1);/ 元素使对象 (3)取得动态数组实际元素个数 count属性 for (int i = 0; i aList.Count;i+ ) /Coount 熟悉确定元素实际个数 Console.WriteLine(aListi); /输出 每个元素 aListi表示数组中第i的元素的值 Student st1 st1=(Student)aList0;/如果数组中存放对象 需要进行 强制转换

3、(4)insert 方法在指定位置插入元素 aList.Insert(0, 5); /在下标为0的位置 插入5 后面的元素自动后移 (5)Remove(object obj);从ArrayList中移除特定对象的第一个匹配项,注意是第一个 aList.Add(a); aList.Add(b); aList.Add(c); aList.Add(d); aList.Add(c); aList.Remove(c); for(int i=0;i 0; i-) aList.Add(i); aList.Sort();/从小到大排序 for(int i=0;iaList.Count ;i+) Console

4、.WriteLine(aListi); 利用以上介绍的方法 编程实现: 定义一个teacher 类,包含姓名和职称和一个输出自己信息的方法,建立arraylist类动态数组将3个teacher类对象的增加到数组中,在索引值为1的位置 插入一个 teacher类对象,删除索引值为1 的元素,最后输出数组中 所有元素的信息 运行结果如下: 代码如下: using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.

5、Threading.Tasks; namespace ConsoleApplication1 class teacher string name, zhicheng; public teacher() Console.WriteLine(*); public teacher( string name,string zhicheng ) this.name = name; this.zhicheng = zhicheng; public void output(teacher p) Console.WriteLine(该老师名字:0 +该老师的职称为:1,p.name ,p.zhicheng )

6、; class Program static void Main(string args) ArrayList aList = new ArrayList(10); teacher a = new teacher(于建,高级教师); teacher b = new teacher(蔡天菊,特级教师); teacher c = new teacher(王小勇,终极教师); teacher d = new teacher(赵旭东,低级教师); aList.Add(a); aList.Add(b); aList.Add(c); aList.Insert(1, d); aList.RemoveAt(1

7、); for (int i = 0; i aList.Count; i+) teacher x = (teacher)aListi; x.output(x); Console.WriteLine(*); Console.Read(); 可参看 教材 154-157页 2.索引器 类中保护数组对象时,可通过索引器实现方便访问 索引器用来访问类中的数组型对象元素 定义索引器与定义属性类似,一般形式为: 修饰符 数据类型 thisint index get /获取数组成员指定索引的值 return ScoreArrayindex; set /利用外部传递的value值给 数组赋值 ScoreArra

8、yindex = value; 观察下面的程序是如何访问 数组成员的 using System; using System.IO; class IndexerClass public int ScoreArray = new int10; /定义公有数组成员 public void output() foreach (int v in ScoreArray) Console.Write(0 , v); public class MainClass public static void Main() IndexerClass b = new IndexerClass(); b.ScoreArra

9、y0=1; /使用公用成员数组 给数组对应索引赋值 b.ScoreArray1=2; Console.WriteLine(0, b.ScoreArray1); /使用公用数组成员 获取对于索引的数组值 Console.WriteLine(0, b.ScoreArray0); b.output(); Console.Read(); 改写上面的程序,仔细观察 索引器的实现,并掌握每句代码的意义 并允许观察结果 using System; class IndexerClass private int ScoreArray = new int10; /可定义为私有数组成员 public int thi

10、sint index / 索引器的声明 通过索引器 访问 具体索引的数组值 get / 检查索引器的范围 if (index = 10) return 0; else return ScoreArrayindex; set if (!(index = 10) ScoreArrayindex = value; public class MainClass public static void Main() IndexerClass b = new IndexerClass(); / 调用索引器初始化数组中的第4个和第6个元素 b3 = 256; /通过数组下标 来给 对象中对应数组赋值 b5 =

11、 1024; for (int i = 0; i = 11; i+) Console.WriteLine(数组元素#0 = 1, i, bi); /通过下标获取对象中 数组成员的只 Console.Read(); 仿照以上例子,利用索引器 实现下面程序 编程: 定义一个Student类,包含学号和姓名两个字段,并定义一个班级类ClassList,包含一个具有5个元素Student数组对象,利用索引器访问该数组对象,并在主函数中 访问。 运行结果如图: 代码如下: using System; using System.Collections; class classlist public stu

12、dent ScoreArray = new student 5; /可定义为私有数组成员 public student thisint index / 索引器的声明 通过索引器 访问 具体索引的数组值 get / 检查索引器的范围 return ScoreArrayindex; set if (!(index = 5) ScoreArrayindex = value; class student string name; int xuehao; public student(string name,int xuehao) this.name = name; this.xuehao = xueh

13、ao; public void output() Console .WriteLine (名字:0+学号:1,name,xuehao ); public class MainClass public static void Main() classlist b = new classlist (); / 调用索引器初始化数组中的第4个和第6个元素 student A = new student(赵旭东,201531); student B = new student(张1, 201579); student C = new student(张2, 201571); student D = ne

14、w student(张三, 201572); student E = new student(张4, 201573); b0 = A; b1 = B; b2 = C; b3 = D; b4 = E; for (int i = 0; i = 11; i+) student x = bi; x.output();/通过下标获取对象中 数组成员的只 Console.Read(); 3.泛型 一般我们定义类或函数 ,其数据成员都指定数据类型,但是为了程序的重用性,我们可以不指明具体数据类型 而是用一个符号来表示,在程序执行的时候 将这个符号替换为具体数据类型,这就是泛型的使用 观察下面的程序 读懂每句

15、代码的意义 using System; using System.IO; class Student int no; string name; public Student(int no1, string name1) no = no1; name = name1; public string Get_msg() return no.ToString() + : + name; public class MainClass public static void Main() Student b = new Student(1001,令?狐?冲?); Console.WriteLine(0, b

16、.Get_msg(); Console.Read(); 我们将上面的代码 中数据类型 进行符号化,就变为泛型,观察下面的程序 using System; using System.IO; class Student /指定数据类型参数 T1,T2 可分别代表不同数据类型 这是泛型类 T1 no; T2 name; public Student(T1 no1, T2 name1) no = no1; name = name1; public void Set_msg(T1 no1, T2 name1) /形参 可以是 参数化类型 这是 泛型方法 no = no1; name = name1; p

17、ublic string Get_msg() return no.ToString() + : + name; public class MainClass public static void Main() Student b = new Student(1001,令狐冲); /注意 创建对象的格式 / 数据成员 1个是int 对于类中的T1 另1个是string 对于类中的T2 Console.WriteLine(0, b.Get_msg(); Student b1=new Student(1001,令狐冲); / 数据成员 1个是string 对于类中的T1 另1个是string 对于类

18、中的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个元素 进行排序,求和,求最大和最小值,并通过主函数进行验证。 可参考教材173 class Data 部分 .课后 熟悉教程 List和Dictionary系统预定义泛型类的使用 定义一个teacher 类,包含姓名和职称和一个输出自己信息的方法,用List创建动态数组将3个teacher类对象的增加到数组中,在索引值为1的位置 插入一个 teacher类对象,删除索引值为1 的元素,最后输出数组中 所有元素的信息(List类 是arraylist的泛型类 ,也就是可以指定数组元素的数据类型,使用方法与arraylist类类似) 实验报告要求 将以上各题的源程序、运行结果写在该题后面,以及实验中遇到的问题和解决问题的方法,以及实验过程中的心得体会,写在下面的空白中。

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

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