




已阅读5页,还剩33页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第九章 数组 集合对象和范型 2 回顾 线程是在共享内存空间中并发的多道执行路径在C 中 是使用System Threading命名空间中的Thread类来创建线程的线程优先级可以更改为ThreadPriority枚举中定义的值C 中的lock关键字是实现线程同步的一种方法同步的线程称为安全线程除非绝对必要 否则不要创建线程安全的代码 因为添加不必要的锁定会降低性能 3 目标 使用System Array对象理解集合对象的特点和优点使用System ArrayList对象使用哈希表对象 4 intscore1 System Console ReadLine score1 intscore2 System Console ReadLine score2 intscore3 System Console ReadLine score3 intscore4 System Console ReadLine score4 intscore5 System Console ReadLine score5 intscore6 System Console ReadLine score6 intscore7 System Console ReadLine score7 第七位学生的分数 第六位学生的分数 第五位学生的分数 第四位学生的分数 第三位学生的分数 第二位学生的分数 第一位学生的分数 score 6 score 5 score 4 score 3 score 1 score 0 score 2 System Array简介3 1 应用程序 数组 存储学员的分数 intscore newint 7 6 7 5 4 3 2 1 在数组的术语中 元素表示数组中存储的值 数组长度指数组中存储的值的总数 数组秩指数组的总维数 5 System Array简介3 2 数组定义 数据类型 数组名称 int MyArray 1 2 3 4 5 6 7 MyArray 0 MyArray 1 MyArray 2 MyArray 6 MyArray 0 604 可以执行各种操作 如存储 检索 排序和反转 System Array 如何简易地执行对数组的操作 6 System Array简介3 3 System Array Array是抽象的基类 提供CreateInstance方法来创建数组 Arrayobj Array CreateInstance typeof string 8 7 System Array的属性和方法 属性 Length 方法 BinarySearch Clear Copy Rank IsReadOnly IsFixedSize CopyTo CreateInstance GetLength GetLowerBound GetUpperBound GetValue IndexOf LastIndexOf Reverse SetValue Sort 8 示例2 1 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 使用GetValue 方法检索数组值 使用SetValue 方法存储字符串 将objNames实例化为字符串对象并且其中存放5个元素 9 示例2 2 Console WriteLine n数组中元素的总数是 0 objNames Length ToString 输出数组秩Console WriteLine n数组秩是 0 objNames Rank ToString 反转数组并输出Array Reverse objNames Console WriteLine n反转数组后 for intctr 0 ctr 5 ctr Console WriteLine 元素 0 1 ctr 1 objNames GetValue ctr 显示objNames数组的长度 显示objNames数组秩 反转数组元素 反转后的数组元素列表 课堂练习 这段代码用Foreach结构怎么写 10 思考和演示 使用 int A 1 2 3 4 5 6 和使用 B CreateInstance typeof string 5 创建的数组 A可以使用B所有的属性和方法吗 教员演示两种数组的差别 11 System Collections简介2 1 ID 工作档案 ID 工作档案 ID 工作档案 ID 工作档案 职员2 职员4 Employee对象的集合 对象组中元素个数未知 并且随时可能要循环 添加和移除 System Collection 12 System Collections简介2 2 System Collection命名空间 13 ArrayList是一个可动态维护长度的集合 可维护动态长度的集合 新增元素 可删除元素 可访问单个元素 object object object object 0 1 2 3 索引 下标 object 4 0 1 2 3 ArrayList 自动调整索引 通过索引访问 14 ArrayList的初始化 usingSystem Collections ArrayListStudents newArrayList ArrayListTeachers newArrayList 5 可以指定长度 引入命名空间 实例化一个对象 常见错误 未引入命名空间 定义时未实例化 引入System Collections命名空间实例化ArrayList对象 15 ArrayList添加元素 intAdd Objectvalue 添加一个对象到集合的末尾 ArrayListstudents newArrayList Studentscofield newStudent Scofield Genders Male 28 越狱 students Add scofield 创建学员对象 添加学员 连续添加三个学员对象并获取集合元素的数目 返回索引 ArrayList Count获取元素数目 建立班级学员的集合 演示 MySchool 16 访问ArrayList元素 类型 ArrayList index 按指定索引 下标 取得对象 Studentstu1 Student students 0 stu1 SayHi 按索引取值 转换为学员对象 ArrayList第一个对象的索引是0 需要类型转换 17 删除ArrayList的元素 ArrayList Remove 对象名 删除指定对象名的对象ArrayList RemoveAt index 删除指定索引的对象ArrayList Clear 清除集合内的所有元素 students RemoveAt 0 students Remove zhang Studentleave Student students 0 leave SayHi 通过索引删除对象 通过指定对象删除 只剩一个对象 剩余的元素会自动调整索引 取得删除后的第一个对象 18 ArrayList的遍历 遍历foreach Objectstuoinstudents StudentstuForeach Student stuo Console WriteLine stuForeach Name 通过对象遍历 打印对象的Name属性 Scofield张靓靓周杰杰 可以使用和数组类似的方式 for inti 0 i students Count i StudentstuFor Student students i Console WriteLine stuFor Name foreach方式 输出结果 类型转换 演示 MySchool 19 常见错误1 students Add scofield students Add zhang students Add jay students RemoveAt 0 students RemoveAt 1 students RemoveAt 2 运行这段代码会发生错误 索引会自动调整删除两个元素后 不再有索引 2 20 常见错误2 Studentscofield newStudent Scofield Genders Male 28 越狱狱 Studentstu2 newStudent Scofield Genders Male 28 越狱狱 打印集合数目 students Remove stu2 打印集合数目 如果Remove stu2 能否删除scofield对象 定义一个对象与前面的值相同 两次的结果都是3 没有删除 为什么 在多态章节中我们将试图改变这一现象 演示 MySchool 21 publicstructStudent publicStudent stringname intage Name name Age age publicstringName publicintAge Studentstu1 newStudent 张三 20 Studentstu2 newStudent 李四 20 Studentstu3 newStudent 王五 20 ArrayListstudents newArrayList students Add stu1 students Add stu2 students Add stu3 常见错误3 staticvoidMain string args 初始化代码将对象加入ArrayList集合foreach Studentstuinstudents StudentmyStudent Student stu myStudent Age 60 foreach Studentstuinstudents Console WriteLine stu Age 遍历显示年龄结果是多少 结构是值类型 定义一个学员结构 使用ArrayList存储学员 遍历修改年龄 演示 MyError 22 ArrayList通过索引获取对象 为什么使用HashTable 能否象索引器那样通过关键字获取该对象呢 动态可维护长度 可通过关键字检索 C 提供一种集合 HashTable Students 周杰杰 SayHi Studentstu1 Student Students 0 第三章中的索引器通过关键字获取对象 23 什么是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 值 键 键和值一一对应 24 使用哈希表 students Add scofield Name scofield 添加元素 Key Value Studentstu2 Student students 周杰杰 stu2 SayHi 通过key获取元素 students Remove 周杰杰 通过key删除元素 给哈希表添加元素 获取哈希表的元素 删除哈希表的元素 需要类型转换 演示 MySchool 25 哈希表的遍历 foreach Objectstuoinstudents Values Studentstu Student stuo Console WriteLine stu Name 如何遍历一个哈希表 Key Value Key Value Key Value Key Value Key Value Values 不能遍历整个对象 而是遍历Values foreach stringnameinstudents Keys Console WriteLine name 可以遍历Keys 26 小结 删除一个ArrayList元素有几种方法 HashTable和ArrayList的主要区别是什么 27 类型安全1 MySchool中添加一个Teacher类 Teacherjacky newTeacher 成龙龙 4 jacky SayHi students Add jacky 能否加入一个Teacher对象 foreach Objectstuoinstudents Studentstu Student stuo Console WriteLine stu Name 遍历这个集合是否有问题 演示 MySchool 28 类型安全2 Student集合 Scofield 张靓靓 周杰杰 成龙龙 添加对象 遍历集合 对象存储不易控制 类型转换容易出错 Teacher对象 运行错误 29 引入命名空间 System Collections GenericListstudents newList 利用List存储班级集合 List的使用 students Add scofield students Add jacky 将Student对象加入班级 将Teacher对象加入班级 编译出错 foreach Studentstuinstudents Console WriteLine stu Name 不需类型转换 遍历List集合 演示 MySchool 只能保存Student对象 30 什么是泛型集合 泛型最常见的用途是创建集合类泛型集合可以约束集合内的元素类型典型泛型集合List Dictionary 表示该泛型集合中的元素类型 List Student对象 Teacher对象 允许添加 不允许添加 Student对象 无需转换类型 31 使用泛型集合List Studentstu1 students 2 stu1 SayHi Students RemoveAt 0 List方式foreach Studentstuinstudents Console WriteLine stu Name List的访问方式与ArrayList相同 使用索引访问 无需类型转换 利用索引删除 遍历时不需要类型转换 32 List与ArrayList 是否有哈希表那样存储
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高校安全月知识题库及答案解析
- 北京高三模拟试题及答案
- 2025年高考英语试卷及答案
- 中级护理员考试题及答案
- 上海幼教考试试题及答案
- 2025年骨科创伤救治演练模拟试卷答案及解析
- 2025年神经科学科神经生理病理考试答案及解析
- 2025年口腔颌面外科实操技术考核答案及解析
- 2025年新《烟草专卖法》基础知识考试题库(附答案)
- 2025年口腔颌面外科手术操作模拟考核试卷答案及解析
- 2025年国家电网有限公司特高压建设分公司招聘10人(第一批)笔试参考题库附带答案详解
- 6.2 人大代表为人民 第二课时 课件 2025-2026学年六年级道德与法治 上册 统编版
- 2025年甘肃省金川集团股份有限公司技能操作人员社会招聘400人考试参考试题及答案解析
- 2025年会议行业研究报告及未来发展趋势预测
- T/CIE 189-2023硫化物全固态锂电池
- 借游戏账号合同5篇
- 《医疗器械监督抽验介绍》
- 2025年中职政治专业资格证面试技巧与答案解析大全
- 炎德·英才大联考长郡中学2026届高三月考试卷(一)生物试卷(含答案)
- 3.4 活动:电路创新设计展示说课稿 2023-2024学年教科版物理九年级上册
- 2025-2026学年人教鄂教版(2024)小学科学三年级上册(全册)教学设计(附目录P137)
评论
0/150
提交评论