《C#程序设计课件》(资料全集).ppt

上传人:sh****n 文档编号:7425080 上传时间:2020-03-21 格式:PPT 页数:26 大小:266KB
返回 下载 相关 举报
《C#程序设计课件》(资料全集).ppt_第1页
第1页 / 共26页
《C#程序设计课件》(资料全集).ppt_第2页
第2页 / 共26页
《C#程序设计课件》(资料全集).ppt_第3页
第3页 / 共26页
点击查看更多>>
资源描述
第四章数组和集合 C net程序设计 本章主要内容 集合类型接口IEnumerable ICollection IList和IDictionary数组Array ArrayList和List泛型类Hashtable类和Dictionary泛型类SortedList和SortedList泛型类队列Queue类和Queue泛型类 堆栈Stack类和Stack泛型类 集合类型概述 集合通常可以分为常用集合 专用集合等类型 常用集合 常用集合有泛型和非泛型之分 非泛型集合是以Object类型为元素集合 如哈希表Hashtable 队列Queue 堆栈Stack 和列表ArrayList 泛型集合类型是非泛型类型的直接模拟 泛型集合包含ArrayList的泛型版List Hashtable的泛型版Dictionary集合等 专用集合 这些集合都具有专门的用途 通常用于处理特定的元素类型 如StringDictionary是将键和值强类型化为字符串而不是Object来实现Hashtable集合类型 集合类型 IEnumerable接口 集合是基于IEnumerable接口 ICollection接口 IList接口 IDictionary接口 或其泛型集合中的相应接口 而IEnumerable接口 ICollection接口是大部分集合类所共同实现的 下面分别介绍IEnumerable接口 ICollection接口 第一种集合是实现IEnumerable接口的集合类 IEnumerable接口只有一个公共方法 IEnumeratorGetEnumerator 该方法返回一个用于foreach简单迭代循环访问集合的枚举数 所有实现了IEnumerable接口的集合类如数组Array ArrayList集合类型等都可以用于foreach循环语句 IEnumerator接口的成员如下表 集合类型 ICollection接口 ICollection接口继承IEnumerable接口 除了继承IEnumerable接口成员外 还有下表所示的成员 集合类型 List接口 List接口表示可按照索引单独访问的对象的非泛型集合接口 IList接口继承了ICollection接口和IEnumerable接口 IList是所有非泛型列表的基接口 IList接口的公共属性与方法如下表 数组Array ArrayList和List泛型类 数组Array类 Array类是所有数组的基类 提供创建 操作 搜索和排序数组的方法 Array类定义语法如下 publicabstractclassArray ICloneable IList ICollection IEnumerable 因此Array类实现IList ICollection IEnumerable ICloneable接口 也就是说 Array类实现了这些接口的方法成员 Array类除了Copy CopyTo外其它常用的方法 Array Clear方法 publicstaticvoidClear Arrayarray intindex intlength Array Clone方法是实现ICloneable接口的方法 Clone方法创建数组Array的浅表副本 数组的浅表副本仅复制数组的元素 无论它们是引用类型还是值类型 但不复制这些引用所引用的对象 新数组中的引用与原始数组中的引用指向相同的对象 数组使用Copy CopyTo方法复制的也是浅表副本 所以这三个复制方法得到的复制的副本都是一样 数组Array ArrayList和List泛型类 publicclassStudent publicstringName publicStudent stringName this Name Name publicclassCloneCopyArray publicstaticvoidMain Studentstu0 newStudent student1 Studentstu1 newStudent student2 Studentstu2 newStudent student3 Student arrStu newStudent stu0 stu1 stu2 Student arrStuClone Student arrStu Clone 克隆数组Student arrStuCopy newStudent arrStu Length Array Copy arrStu arrStuCopy arrStu Length 拷贝数组Console WriteLine 原来数组内容 PrintIndexAndValues arrStu Console WriteLine 克隆数组内容 PrintIndexAndValues arrStuClone Console WriteLine 改变克隆数组内容之前 例 演示数组Array的Copy和Clone方法的使用 CloneCopyArray项目代码 Console WriteLine arrStu 2 Name 0 arrStu 2 Name Console WriteLine arrStuClone 2 Name 0 arrStuClone 2 Name Console WriteLine arrStuCopy 2 Name 0 arrStuCopy 2 Name arrStuClone 2 Name student2CloneNew arrStuCopy 2 Name student2CopyNew Console WriteLine 改变克隆数组内容之后 Console WriteLine arrStu 2 Name 0 arrStu 2 Name Console WriteLine arrStuClone 2 Name 0 arrStuClone 2 Name Console WriteLine arrStuCopy 2 Name 0 arrStuCopy 2 Name publicstaticvoidPrintIndexAndValues ArraymyArray for inti myArray GetLowerBound 0 i myArray GetUpperBound 0 i Console WriteLine t 0 t 1 i myArray GetValue i arrStuClone是使用clone方法复制的student类对象数组 arrStuCopy是使用copy方法复制的student类对象数组 由于它们都是复制的浅表副本 所以在三个数组的引用都指向相同的student类对象数组 ArrayList类 Array用作所有数组的基类 而ArrayList是较为复杂的数组 ArrayList类和Array类一样都实现IList ICollection IEnumerable ICloneable接口 ArrayList类除了所实现的IList ICollection IEnumerable ICloneable接口的方法成员 还包含下面主要属性和方法 使用ArrayList类Add AddRange和ToArray方法的项目ArrayListSample代码 usingSystem usingSystem Collections publicclassSamplesArrayList publicstaticvoidMain ArrayListmyAL newArrayList 创建和初始化ArrayList myAL Add The 添加一个元素myAL AddRange newstring quick brown fox jumped over the lazy dog 添加一组元素PrintIndexAndValues myAL 显示ArrayList的值String myArr String myAL ToArray typeof string 将元素复制数组PrintIndexAndValues myArr 显示数组内容 publicstaticvoidPrintIndexAndValues ArrayListmyList inti 0 foreach ObjectoinmyList Console Write t 0 t 1 i o publicstaticvoidPrintIndexAndValues String myArr for inti 0 i myArr Length i Console Write t 0 t 1 i myArr i List泛型类 List泛型类是ArrayList类的泛型等效类 表示可通过索引访问的强类型列表 所谓的强类型 是指创建列表List时指定集合类型 而不是ArrayList的object集合类型 这样对于值类型的List泛型类来说 无需装箱和取消装箱或转换 ListTSample项目的代码示例演示List泛型类 usingSystem usingSystem Collections Generic publicclassExample publicstaticvoidMain 创建string的List泛型实例 创建列表时指定集合类型为stringListdinosaurs newList Console WriteLine nCapacity 0 dinosaurs Capacity 显示List容量dinosaurs Add Tyrannosaurus 向List添加dinosaurs Add Amargasaurus dinosaurs Add Mamenchisaurus dinosaurs Add Deinonychus dinosaurs Add Compsognathus foreach stringdinosaurindinosaurs Console WriteLine dinosaur Console WriteLine nCapacity 0 dinosaurs Capacity Console WriteLine Count 0 dinosaurs Count Console WriteLine nContains Deinonychus 0 dinosaurs Contains Deinonychus 判断列表是否包含 Deinonychus dinosaurs Insert 2 Compsognathus 在位置插入 Compsognathus foreach stringdinosaurindinosaurs Console WriteLine dinosaur Console WriteLine ndinosaurs 3 0 dinosaurs 3 使用位置索引dinosaurs Remove Compsognathus 删除 Compsognathus foreach stringdinosaurindinosaurs Console WriteLine dinosaur dinosaurs TrimExcess 根据集合数量缩减容量Console WriteLine nTrimExcess nCapacity 0 dinosaurs Capacity Console WriteLine Count 0 dinosaurs Count dinosaurs Clear 清空Console WriteLine nClear nCapacity 0 dinosaurs Capacity Console WriteLine Count 0 dinosaurs Count List泛型类 代码分析 使用默认构造函数创建一个空的字符串类型的List泛型列表 随后显示Capacity属性 然后使用Add方法添加若干个项 添加的项被列出 Capacity属性会同Count属性一起再次显示 指示已根据需要增加了容量 使用Contains方法测试该列表中是否存在某个项 使用Insert方法在列表的中间插入一个新项 然后再次显示列表的内容 默认的Item属性 C 中的索引器 用于检索项 Remove方法用于移除前面添加的重复项的第一个实例 然后 该示例再次显示内容 Remove方法总是移除它所遇到的第一个实例 使用TrimExcess方法减小容量以匹配计数 然后显示Capacity和Count属性 如果未用容量已经小于总容量的10 则列表容量不会进行调整 使用Clear方法移除列表中的所有项 并显示Capacity和Count属性 数组Array ArrayList和List泛型类 List ArrayList类与Array数组的区别 Array的容量是固定的 而ArrayList的容量可根据需要自动扩充 如果更改了Capacity属性的值 则可以自动进行内存重新分配和元素复制 ArrayList提供添加 插入或移除某一范围元素的方法 在Array中 只能一次获取或设置一个元素的值 使用Synchronized方法很容易创建ArrayList的同步版本 Array将实现同步的任务留给了用户 ArrayList提供将只读和固定大小包装返回到集合的方法 而Array不提供 可以设置Array的下限 但ArrayList的下限始终为零 Array可以具有多个维度 而ArrayList始终只是一维的 特定类型 不包括Object 的数组Array的性能优于ArrayList 这是因为ArrayList的元素属于Object类型 所以在存储或检索值类型时通常发生装箱和取消装箱操作 不过 在不需要重新分配时 即最初的容量十分接近列表的最大容量 List泛型类的性能与同类型的数组十分相近 需要数组的大多数情况都可以改为使用ArrayList或List泛型类 它们更容易使用 并且一般与相同类型的数组具有相近的性能 尽量使用List类 而不是使用ArrayList类或自己编写强类型包装集合 数组Array ArrayList和List泛型类 Hashtable Dictionary和SortedList集合类 Hashtable Dictionary和SortedList集合类都实现IDictionary ICollection IEnumerable ICloneable接口 其中IDictionary接口表示键 值对的集合接口 或者说Hashtable Dictionary和SortedList都是关于键 值对的集合 IDictionary接口是键 值对的非通用集合的基接口 每个元素都是一个存储在DictionaryEntry对象中的键 值对 IDictionary接口继承了ICollection IEnumerable接口 除了ICollection IEnumerable接口成员外 还有以下成员 IDictionary接口成员 usingSystem Collections 使用Hashtable类的项目HashtableSample的代码 publicclassSamplesHashtable publicstaticvoidMain Hashtableht newHashtable 创建Hashtable实例htht Add N01 张三 添加将带有指定键和值的元素添加到htht Add N02 李四 ht Add N03 王五 Console WriteLine ht Count 0 ht Count 显示Hashtable的属性CountPrintKeysAndValues ht publicstaticvoidPrintKeysAndValues Hashtableht foreach DictionaryEntrydeinht 显示Hashtable的键和值Console Write 0 1 de Key de Value 使用Hashtable的Add Objectkey Objectvalue 方法集合添加元素 Hashtable的元素是一个键 值对 元素类型既不是键的类型 也不是值的类型 而是DictionaryEntry类型 因此foreach循环语句使用foreach DictionaryEntrydeinht DictionaryEntry是结构类型 具有object类型的键属性Key和object类型的值属性Key 使用Dictionary泛型类DictionarySample项目代码 publicclassSample publicstaticvoidMain 创建键和值都为string的dictionary对象实例openWith DictionaryopenWith newDictionary 为dictionary实例openWith添加元素 其中键值唯一 有些值是重复的openWith Add txt notepad exe openWith Add bmp paint exe openWith Add dib paint exe openWith Add rtf wordpad exe try 插入具有重复键的元素因起异常openWith Add txt winword exe catch ArgumentException Console WriteLine AnelementwithKey txt alreadyexists 使用键索引得到其值Console WriteLine Forkey rtf value 0 openWith rtf openWith rtf winword exe 使用键索引设置其值Console WriteLine Forkey rtf value 0 openWith rtf openWith doc winword exe 如果该键不存在 添加新key value元素 try 如果没有该键 取其值引起异常Console WriteLine Forkey tif value 0 openWith tif catch KeyNotFoundException Console WriteLine Key tif isnotfound stringvalue if openWith TryGetValue tif outvalue 该方法更有效 不引起异常Console WriteLine Forkey tif value 0 value else Console WriteLine Key tif isnotfound if openWith ContainsKey ht 判断是否含有该键openWith Add ht hypertrm exe Console WriteLine Valueaddedforkey ht 0 openWith ht foreach KeyValuePairkvpinopenWith 遍历该字典Console WriteLine Key 0 Value 1 kvp Key kvp Value 取该字典值属性 值集合ValueCollection是强类型集合 本例string类型 Dictionary ValueCollectionvalueColl openWith Values foreach stringsinvalueColl 遍历该字典值集合ValueCollectionConsole WriteLine Value 0 s 取该字典的键属性 键集合KeyCollection是强类型集合 本例string类型 Dictionary KeyCollectionkeyColl openWith Keys foreach stringsinkeyColl 遍历该字典键集合ValueCollection Console WriteLine Key 0 s Console WriteLine n删除键为 doc 的这对元素 openWith Remove doc if openWith ContainsKey doc Console WriteLine Key doc isnotfound 代码分析 使用Item属性 即索引器 来检索值 当请求的键不存在时将引发KeyNotFoundException 与键相关联的值可被替换 当程序必须经常尝试字典中不存在的键值时 使用TryGetValue方法作为一种更有效的方法来检索值 使用ContainsKey方法在调用Add方法之前测试某个键是否存在 使用Keys属性和Values属性来单独枚举键和值 使用Remove方法删除集合中的一个元素 Queue类和Queue泛型类 Queue类和Queue泛型类都是先进先出集合类 它们实现IEnumerable ICollection和ICloneable等接口 Queue泛型类还实现了IEnumerable和ICollection泛型接口 队列在按接收顺序存储消息方面可以方便地进行顺序处理 此类将队列作为循环数组实现 存储在Queue中的对象在一端插入 从另一端移除 Queue接受空引用作为有效值并且允许重复的元素 可以对Queue及其元素执行三种主要操作 Enqueue将一个元素添加到Queue的末尾 Dequeue从Queue的开始处移除最旧的元素 Peek从Queue的开始处返回最旧的元素 但不将其从Queue中移除 下面是使用队列Queue泛型类的QueueSample项目的代码 usingSystem Collections Generic classExample publicstaticvoidMain Queuenumbers newQueue numbers Enqueue one numbers Enqueue two numbers Enqueue three numbers Enqueue four numbers Enqueue five foreach stringnumberinnumbers Console WriteLine number Console WriteLine nDequeuing 0 numbers Dequeue Console WriteLine Peekatnextitem 0 numbers Peek Console WriteLine Dequeuing 0 numbers Dequeue 使用ToArray方法将队列元素复制到数组 然后将该数组 传递给接受IEnumerable的Queue构造函数以创建队列副本QueuequeueCopy newQueue numbers ToArray foreach stringnumberinqueueCopy Console WriteLine number 创建一个大小是队列大小两倍的数组string array2 newstring numbers Count 2 使用CopyTo方法从数组中间开始复制数组元素numbers CopyTo array2 numbers Count 使用接受IEnumerable的Queue构造函数创建第二个队列QueuequeueCopy2 newQueue array2 foreach stringnumberinqueueCopy2 Console WriteLine number Console WriteLine nqueueCopy Contains four 0 queueCopy Contains four queueCopy Clear Console WriteLine nqueueCopy Count 0 queueCopy Count 使用Enqueue方法将五个字符串加入队列进行排队 使用Dequeue方法使第一个字符串出列 使用Peek方法查找队列中的下一项 使用ToArray方法创建一个数组并将队列元素复制到该数组 然后将该数组传递给接受IEnumerable的Queue构造函数以创建队列副本 并显示副本的元素 创建一个大小是队列大小两倍的数组 并使用CopyTo方法从数组中间开始复制数组元素 使用Contains方法显示字符串 four 在第一个队列副本中 然后使用Clear方法清除此副本 并由Count属性显示该队列为空 堆栈Stack类和Stack泛型类 堆栈集合类型Stack类和Stack泛型类都是后进先出集合类 它们实现IEnumerable ICollection和ICloneable等接口 Stack泛型类还实现了IEnumerable和ICollection泛型接口 可以使用堆栈和队列存储临时信息 如果需要按照信息存储在集合中的顺序来访问这些信息 使用队列Queue 如果需要以相反的顺序访问这些信息 使用堆栈Stack Stack常用于在调用其他过程期间保留变量状态 可以对Stack及其元素执行三种主要操作 Push可在Stack的顶部插入一个元素 Pop可在Stack的顶部移除一个元素 Peek可返回处于Stack顶部的元素 但不将其从Stack上移除 classExample publicstaticvoidMain Stacknumbers newStack numbers Push one numbers Push two foreach stringnumberinnumbers Console WriteLine number Console WriteLine nPopping 0 numbers Pop Console WriteLine Peekatnextitem 0 numbers Peek 使用ToArray方法创建数组并将堆栈元素复制到其中 然后将数组传递给具有IEnumerable的Stack构造函数 Stackstack2 newStack numbers ToArray foreach stringnumberinstack2 Console WriteLine number Console WriteLine stack2 Contains four 0 stack2 Contains four stack2 Clear Console WriteLine nstack2 Count 0 stack2 Count 用堆栈Stack泛型类的几个方法 下面是StackSample项目的代码
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!