11 集合.pptVIP

  • 3
  • 0
  • 约 14页
  • 2016-08-04 发布于河南
  • 举报
11 集合

Softeem Consultancy Service 集合 Softeem Consultancy Service 集合 ID:SCSN001-NBS * 集合 * 内容提要 集合 泛型 * 集合 * 类型安全和性能 集合的项目需要运行时的类型转换 运行时类型转换的缺点 类型是在运行时发现,而不是编译时 运行时类型转换代价高 指定类型的集合可以避免类型转换的性能消耗 在System.Collections.Specialized 命名空间中 通过继承System.Collections和添加指定类型的项可以创建自定义的指定类型集合 * 集合 * 集合 集合可以在项目中存储各种类型的数据 System.Collections 命名空间 * 集合 * System.Collections.Generic ICollectionT IListT IDictionaryT * 集合 * 集合类关系图例 * 集合 * IList IList – 代表有序对象集合的类 实现IList的类 List IList方法: Add, Clear, Contains, Insert, IndexOf, Remove和RemoveAt * 集合 * 练习: IList的使用 IListstring list = new Liststring(); list.Add(abc); list.Add(hello); foreach (string c in list ) { Console.WriteLine(c); } * 集合 * Dictionaries IDictionary是包含键和值对的类的集合 实现IDictionary接口的类包括 SortedList, SortedDictionary和Dictionary Methods包括: Add, Clear, Contains, GetEnumerator和 Remove * 集合 * SortedList SortedList 两个数组 一个数组存储键,另一个存储值 Count 属性 –SortedList中元素的数目 打印SortedList的键和值对 IDictionarystring, int mySL = new SortedListstring, int(); // Add an entry with a key = First and a value = 1 mySL.Add(First, 1); // Increment the value of the entry whose key = First mySL[First] = (Int32)mySL[First] + 1; for ( int i = 0; i myList.Count; i++ ) { Console.WriteLine( \t{0}:\t{1}, myList.GetKey(i), myList.GetByIndex(i) ); } * 集合 * SortedList SortedListstring, Teacher table = new SortedListstring, Teacher(); table.Add(a1, new Teacher(laowang)); table.Add(a2, new Teacher(laozhang)); //遍历Hashtable的关键字 foreach (string key in table.Keys) { //打印出Hashtable的某项值 Teacher t = (Teacher)table[key]; Console.WriteLine({0}:{1}, key, t.getName()); } * 集合 * 集合使用指南 下列情况使用集合代替数组: 集合的使用更灵活 集合可以只读,数组总是可以修改 使用集合可以提高效率 * 集合 * 总结 集合 泛型 * 集合 * 练习 创建几个学生对象, 将学生按名字作为关键字保存到一个泛型SortList中,然后遍历输出. 将属性与索引的练习用集合或字典实现. Softeem Consultancy Service 集合 Softeem Consultancy Service

文档评论(0)

1亿VIP精品文档

相关文档