集合对象和范型.ppt

上传人:rrsccc 文档编号:8950871 上传时间:2021-01-26 格式:PPT 页数:38 大小:924KB
返回 下载 相关 举报
集合对象和范型.ppt_第1页
第1页 / 共38页
集合对象和范型.ppt_第2页
第2页 / 共38页
集合对象和范型.ppt_第3页
第3页 / 共38页
集合对象和范型.ppt_第4页
第4页 / 共38页
集合对象和范型.ppt_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《集合对象和范型.ppt》由会员分享,可在线阅读,更多相关《集合对象和范型.ppt(38页珍藏版)》请在三一文库上搜索。

1、数组、集合对象和范型,回顾,线程是在共享内存空间中并发的多道执行路径 在 C# 中,是使用 System.Threading 命名空间中的 Thread 类来创建线程的 线程优先级可以更改为 ThreadPriority 枚举中定义的值 C# 中的 lock 关键字是实现线程同步的一种方法 同步的线程称为安全线程 除非绝对必要,否则不要创建线程安全的代码,因为添加不必要的锁定会降低性能,目标,使用System.Array 对象 理解集合对象的特点和优点 使用System.ArrayList 对象 使用哈希表对象,int score1; System.Console.ReadLine(score

2、1) int score2; System.Console.ReadLine(score2) int score3; System.Console.ReadLine(score3) int score4; System.Console.ReadLine(score4) int score5; System.Console.ReadLine(score5) int score6; System.Console.ReadLine(score6) int score7; System.Console.ReadLine(score7),第七位学生的分数,第六位学生的分数,第五位学生的分数,第四位学生的

3、分数,第三位学生的分数,第二位学生的分数,第一位学生的分数,score 6,score 5,score 4,score 3,score 1,score 0,score 2,System.Array 简介 3-1,应用程序,数 组,存储学员的分数,int score = new int7;,6,7,5,4,3,2,1,在数组的术语中,元素表示数组中存储的值,数组长度指数组中存储的值的总数,数组秩指数组的总维数,System.Array 简介 3-2,数组定义:数据类型 数组名称;,int MyArray = 1,2,3,4,5,6,7;,MyArray0, MyArray1, MyArray2M

4、yArray6,MyArray 0 = 604,可以执行各种操作,如存储、检索、排序和反转,System . Array,如何简易地执行对数组的操作?,System.Array 简介 3-3,System.Array,Array是抽象的基类,提供 CreateInstance 方法来创建数组,Array obj = Array.CreateInstance(typeof(string),8);,System.Array 的属性和方法,属性,Length,方法,BinarySearch,Clear,Copy,Rank,IsReadOnly,IsFixedSize,CopyTo,CreateIns

5、tance,GetLength,GetLowerBound,GetUpperBound,GetValue,IndexOf,LastIndexOf,Reverse,SetValue,Sort,示例 2-1,static void Main(string args) /构建 objNames 数组 Array objNames = Array.CreateInstance(typeof(string),5); /初始化值 objNames.SetValue(“A,0); objNames.SetValue(“B,1); objNames.SetValue(“C,2); objNames.SetVa

6、lue(“D,3); objNames.SetValue(“E,4); Console.WriteLine(“数组值); for(int ctr = 0 ; ctr 5; ctr+) Console.WriteLine(“元素 0: 1,ctr+1, objNames.GetValue(ctr); ,使用 GetValue() 方法检索数组值,使用 SetValue() 方法存储字符串,将 objNames 实例化为字符串对象并且其中存放 5 个元素,示例 2-2,Console.WriteLine(“n数组中元素的总数是 0,objNames.Length.ToString(); /输出数组

7、秩 Console.WriteLine(n数组秩是 0,objNames.Rank.ToString(); /反转数组并输出 Array.Reverse(objNames); Console.WriteLine(“n反转数组后); for(int ctr = 0 ; ctr 5; ctr+) Console.WriteLine(“元素 0: 1,ctr+1, objNames.GetValue(ctr); ,显示 objNames数组的长度,显示 objNames数组秩,反转数组元素,反转后的数组元素列表,课堂练习: 这段代码用Foreach结构怎么写?,思考和演示,使用: int A = 1

8、,2,3,4,5,6 和使用: B.CreateInstance (typeof(string),5) 创建的数组,A可以使用B所有的属性和方法吗?,教员演示两种数组的差别,System.Collections 简介 2-1,ID,工作档案,ID,工作档案,ID,工作档案,ID,工作档案,职员 2,职员 4,Employee 对象的集合,对象组中元素个数未知,并且随时可能要循环、添加和移除,System.Collection,System.Collections 简介 2-2,System.Collection 命名空间,ArrayList是一个可动态维护长度的集合,可维护动态长度的集合,新增

9、元素,可删除元素,可访问单个元素,object,object,object,object,0,1,2,3,索引(下标),object,4,0,1,2,3,ArrayList,自动调整索引,通过索引访问,ArrayList的初始化,using System.Collections; ArrayList Students = new ArrayList(); ArrayList Teachers = new ArrayList(5),可以指定长度,引入命名空间,实例化一个对象,常见错误,未引入命名空间,定义时未实例化,引入System.Collections命名空间 实例化ArrayList对象,

10、ArrayList添加元素,int Add(Object value) /添加一个对象到集合的末尾,ArrayList students = new ArrayList(); Student scofield = new Student(Scofield, Genders.Male, 28, 越狱); students.Add(scofield);,创建学员对象,添加学员,连续添加三个学员对象 并获取集合元素的数目,返回索引,ArrayList.Count获取元素数目,建立班级学员的集合,演示:MySchool,访问ArrayList元素,(类型) ArrayListindex /按指定索引(

11、下标)取得对象,Student stu1 = (Student)students0; stu1.SayHi(); ,按索引取值,转换为学员对象,ArrayList第一个对象的索引是0,需要类型转换,删除ArrayList的元素,ArrayList.Remove(对象名) /删除指定对象名的对象 ArrayList.RemoveAt(index) /删除指定索引的对象 ArrayList.Clear() /清除集合内的所有元素,students.RemoveAt(0); students.Remove(zhang); Student leave = (Student)students0; lea

12、ve.SayHi();,通过索引删除对象,通过指定对象删除,只剩一个对象,剩余的元素会自动调整索引,取得删除后的第一个对象,ArrayList 的遍历,/遍历 foreach (Object stuo in students) Student stuForeach = (Student)stuo; Console.WriteLine(stuForeach.Name); ,通过对象遍历,打印对象的Name属性,Scofield 张靓靓 周杰杰,可以使用和数组类似的方式,for (int i = 0; i students.Count; i+) Student stuFor = (Student)

13、studentsi; Console.WriteLine(stuFor.Name); ,foreach 方式,输出结果,类型转换,演示:MySchool,常见错误1,/ students.Add(scofield); students.Add(zhang); students.Add(jay); students.RemoveAt(0); students.RemoveAt(1); students.RemoveAt(2);,运行这段代码会发生错误?,索引会自动调整删除两个元素后,不再有索引“2“,常见错误2,Student scofield = new Student(Scofield, G

14、enders.Male, 28, 越狱狱); Student stu2 = new Student(Scofield, Genders.Male, 28, 越狱狱); /打印集合数目 students.Remove(stu2); /打印集合数目 ,如果Remove(stu2),能否删除scofield对象?,定义一个对象与前面的值相同,两次的结果都是3,没有删除,为什么? 在多态章节中我们将试图改变这一现象,演示:MySchool,public struct Student public Student(string name, int age) Name = name; Age = age;

15、 public string Name; public int Age; ,Student stu1 = new Student(张三,20); Student stu2 = new Student(李四, 20); Student stu3 = new Student(王五, 20); ArrayList students = new ArrayList(); students.Add(stu1); students.Add(stu2); students.Add(stu3);,常见错误3,static void Main(string args) /初始化代码将对象加入ArrayList集

16、合 foreach (Student stu in students) Student myStudent = (Student)stu; myStudent.Age = 60; foreach (Student stu in students) Console.WriteLine(stu.Age); ,遍历显示年龄结果是多少?,结构是值类型,定义一个学员结构,使用ArrayList存储学员,遍历修改年龄,演示:MyError,ArrayList通过索引获取对象,为什么使用HashTable,能否象索引器那样通过关键字获取该对象呢?,动态可维护长度,可通过关键字检索,C#提供一种集合,Hash

17、Table,Students“周杰杰.SayHi();,Student stu1 = (Student)Students0;,第三章中的索引器通过关键字获取对象,什么是HashTable,ArrayList 每个元素对应一个索引 HashTable 通常称为哈希表 根据键(Key)可以查找到相应的值(Value),object,object,object,object,object,0,1,2,3,ArrayList,索引,Key,Value,Key,Value,Key,Value,Key,Value,Key,Value,HashTable,值,键,键和值一一对应,使用哈希表,students

18、.Add(scofield.Name , scofield);,添加元素,Key,Value, Student stu2 = (Student)students“周杰杰; stu2.SayHi(); ,通过key获取元素,students.Remove(“周杰杰); ,通过key删除元素,给哈希表添加元素,获取哈希表的元素,删除哈希表的元素,需要类型转换,演示:MySchool,哈希表的遍历,foreach (Object stuo in students.Values) Student stu = (Student)stuo; Console.WriteLine(stu.Name); ,如何

19、遍历一个哈希表?,Key,Value,Key,Value,Key,Value,Key,Value,Key,Value,Values,不能遍历整个对象,而是遍历Values,foreach (string name in students.Keys) Console.WriteLine(name); ,可以遍历Keys,小结,删除一个ArrayList元素有几种方法? HashTable和ArrayList的主要区别是什么?,类型安全1,MySchool中添加一个Teacher类,Teacher jacky = new Teacher(成龙龙, 4); jacky.SayHi(); studen

20、ts.Add(jacky);,能否加入一个Teacher对象?,foreach (Object stuo in students) Student stu = (Student)stuo; Console.WriteLine(stu.Name); ,遍历这个集合是否有问题?,演示:MySchool,类型安全2,Student集合,Scofield,张靓靓,周杰杰,成龙龙,添加对象,遍历集合,对象存储不易控制,类型转换容易出错,Teacher对象,运行错误,引入命名空间:System.Collections.Generic List students = new List(); 利用List存储

21、班级集合,List的使用,students.Add(scofield); students.Add(jacky);,将Student对象加入班级,将Teacher对象加入班级,编译出错,foreach (Student stu in students) Console.WriteLine(stu.Name); ,不需类型转换,遍历List集合,演示:MySchool,只能保存Student对象,什么是泛型集合,泛型最常见的用途是创建集合类 泛型集合可以约束集合内的元素类型 典型泛型集合List,Dictionary 、表示该泛型集合中的元素类型,List,Student对象,Teacher对象

22、,允许添加,不允许添加,Student对象,无需转换类型,使用泛型集合List,Student stu1 = students2; stu1.SayHi(); Students.RemoveAt(0); /List 方式 foreach (Student stu in students) Console.WriteLine(stu.Name); ,List的访问方式与ArrayList相同,使用索引访问,无需类型转换,利用索引删除,遍历时不需要类型转换,List 与 ArrayList,是否有哈希表那样存储Key和Value形式的泛型集合呢?,访问 List 与 ArrayList 的对比,D

23、ictionary概述,Dictionary具有List相同的特性 约束集合中元素类型 编译时检查类型约束 无需装箱拆箱操作 与哈希表类似存储Key和Value的集合,Dictionary students = new Dictionary();,利用Dictionary存储学员集合,Key存储String类型,value存储Student类型,Dictionary的使用,students.Add(scofield.Name, scofield); student stu2 = students周杰杰; students.Remove(周杰杰);,添加一对Key/Value,通过Key获取元素

24、,通过Key删除元素,/Dictionary 方式 foreach (Student student in students.Values) Console.WriteLine(student.Name); ,遍历Values,Dictionary的访问方式与哈希表相同,演示:MySchool,Dictionary与哈希表,访问 Dictionary 与 哈希表 的对比,泛型的重要性,泛型集合与传统集合相比类型更安全 泛型集合无需装箱拆箱操作 泛型的重要性 泛型是未来五年的主流技术之一 解决了很多需要繁琐操作的问题 提供了更好的类型安全性 CLR 支持泛型 后续课程学习泛型接口,总结,哈希表如何获取一个元素的Value? ArrayList与哈希表存取对象需要什么操作? List中的T表示什么? 泛型集合与传统集合获取元素时的区别?,

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

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1