《c#程序设计课件》(资料全集)c#

上传人:tian****1990 文档编号:72601263 上传时间:2019-01-23 格式:PPT 页数:26 大小:266.01KB
返回 下载 相关 举报
《c#程序设计课件》(资料全集)c#_第1页
第1页 / 共26页
《c#程序设计课件》(资料全集)c#_第2页
第2页 / 共26页
《c#程序设计课件》(资料全集)c#_第3页
第3页 / 共26页
《c#程序设计课件》(资料全集)c#_第4页
第4页 / 共26页
《c#程序设计课件》(资料全集)c#_第5页
第5页 / 共26页
点击查看更多>>
资源描述

《《c#程序设计课件》(资料全集)c#》由会员分享,可在线阅读,更多相关《《c#程序设计课件》(资料全集)c#(26页珍藏版)》请在金锄头文库上搜索。

1、第四章 数组和集合,C#.net程序设计,本章主要内容,集合类型接口IEnumerable、ICollection、IList和IDictionary 数组Array、ArrayList和List泛型类 Hashtable类和Dictionary泛型类 SortedList和SortedList泛型类 队列Queue类和Queue泛型类,堆栈Stack类和Stack 泛型类,集合类型概述,集合通常可以分为常用集合, 专用集合等类型: 常用集合。常用集合有泛型和非泛型之分。非泛型集合是以Object 类型为元素集合,如哈希表Hashtable、队列Queue、堆栈Stack、和列表ArrayLi

2、st,。泛型集合类型是非泛型类型的直接模拟。泛型集合包含ArrayList的泛型版List,Hashtable的泛型版Dictionary集合等。 专用集合。这些集合都具有专门的用途,通常用于处理特定的元素类型,如 StringDictionary是将键和值强类型化为字符串而不是Object来实现Hashtable集合类型。,集合类型,IEnumerable接口,集合是基于IEnumerable接口、ICollection接口、IList接口、IDictionary接口,或其泛型集合中的相应接口,而IEnumerable接口、ICollection接口是大部分集合类所共同实现的。下面分别介绍I

3、Enumerable接口、ICollection接口。 第一种集合是实现IEnumerable接口的集合类,IEnumerable接口只有一个公共方法:IEnumerator GetEnumerator() 该方法返回一个用于foreach简单迭代循环访问集合的枚举数。所有实现了IEnumerable接口的集合类如数组Array,ArrayList集合类型等都可以用于foreach循环语句。IEnumerator接口的成员如下表。,集合类型,ICollection 接口,ICollection 接口继承IEnumerable接口,除了继承IEnumerable接口成员外,还有下表所示的成员。,

4、集合类型,List 接口,List 接口表示可按照索引单独访问的对象的非泛型集合接口。IList 接口继承了ICollection接口和IEnumerable接口,IList是所有非泛型列表的基接口。IList接口的公共属性与方法 如下表:,数组Array、ArrayList和List泛型类,数组Array类,Array 类是所有数组的基类,提供创建、操作、搜索和排序数组的方法,Array 类定义语法如下:public abstract class Array : ICloneable, IList, ICollection, IEnumerable。因此Array类实现IList, ICol

5、lection, IEnumerable,ICloneable接口,也就是说,Array类实现了这些接口的方法成员。 Array类除了Copy,CopyTo外其它常用的方法: Array.Clear方法:public static void Clear (Array array, int index,int length) Array.Clone方法是实现ICloneable接口的方法,Clone方法创建数组Array的浅表副本,数组的浅表副本仅复制数组的元素(无论它们是引用类型还是值类型),但不复制这些引用所引用的对象。新数组中的引用与原始数组中的引用指向相同的对象。数组使用Copy,Cop

6、yTo方法复制的也是浅表副本。所以这三个复制方法得到的复制的副本都是一样。,数组Array、ArrayList和List泛型类,public class Student public string Name; public Student(string Name) this.Name = Name; public class CloneCopyArray public static void Main() Student stu0 = new Student(“student1“); Student stu1 = new Student(“student2“); Student stu2 =

7、new Student(“student3“); Student arrStu = new Student stu0, stu1, stu2; Student arrStuClone = (Student)arrStu.Clone();/ 克隆数组 Student arrStuCopy = new StudentarrStu.Length; Array.Copy(arrStu,arrStuCopy, arrStu.Length);/拷贝数组 Console.WriteLine(“原来数组内容“); PrintIndexAndValues(arrStu); Console.WriteLine(“

8、克隆数组内容:“); PrintIndexAndValues(arrStuClone); Console.WriteLine(“改变克隆数组内容之前“);,例:演示数组Array的Copy和Clone方法的使用, CloneCopyArray项目代码:,Console.WriteLine(“arrStu2.Name: 0“, arrStu2.Name); Console.WriteLine(“arrStuClone2.Name: 0“, arrStuClone2.Name); Console.WriteLine(“arrStuCopy2.Name: 0“, arrStuCopy2.Name);

9、 arrStuClone2.Name = “student2CloneNew“; /arrStuCopy2.Name = “student2CopyNew“; Console.WriteLine(“改变克隆数组内容之后“); Console.WriteLine(“arrStu2.Name: 0“, arrStu2.Name); Console.WriteLine(“arrStuClone2.Name: 0“, arrStuClone2.Name); Console.WriteLine(“arrStuCopy2.Name: 0“, arrStuCopy2.Name); public static

10、 void PrintIndexAndValues(Array myArray) for (int i = myArray.GetLowerBound(0); i = myArray.GetUpperBound(0); i+) Console.WriteLine(“t0:t1“, i, myArray.GetValue(i); arrStuClone是使用clone方法复制的student类对象数组,arrStuCopy是使用copy方法复制的student类对象数组,由于它们都是复制的浅表副本,所以在三个数组的引用都指向相同的student类对象数组。,ArrayList 类,Array用作

11、所有数组的基类,而ArrayList是较为复杂的数组。ArrayList 类和Array 类一样都实现IList, ICollection, IEnumerable, ICloneable接口。 ArrayList类除了所实现的IList, ICollection, IEnumerable, ICloneable接口的方法成员,还包含下面主要属性和方法:,使用ArrayList类Add、AddRange和ToArray方法的项目ArrayListSample代码: using System;using System.Collections; public class SamplesArrayL

12、ist public static void Main() ArrayList myAL = new ArrayList();/ 创建和初始化ArrayList. myAL.Add(“The“);/添加一个元素 myAL.AddRange(new string “quick“, “brown“, “fox“, “jumped“, “over“, “the“, “lazy“, “dog“ );/添加一组元素 PrintIndexAndValues(myAL); / 显示ArrayList的值 String myArr = (String)myAL.ToArray(typeof(string);

13、/将元素复制数组 PrintIndexAndValues(myArr); / 显示数组内容 public static void PrintIndexAndValues(ArrayList myList) int i = 0; foreach (Object o in myList) Console.Write(“t0:t1“, i+, o); public static void PrintIndexAndValues(String myArr) for (int i = 0; i myArr.Length; i+) Console.Write(“t0:t1“, i, myArri); ,L

14、ist 泛型类,List 泛型类是 ArrayList 类的泛型等效类,表示可通过索引访问的强类型列表。所谓的强类型,是指创建列表List时指定集合类型,而不是ArrayList的object集合类型,这样对于值类型的List泛型类来说,无需装箱和取消装箱或转换。 ListTSample项目的代码示例演示 List 泛型类: using System; using System.Collections.Generic; public class Example public static void Main() /创建string的List泛型实例,创建列表时指定集合类型为string Lis

15、t dinosaurs = new List(); Console.WriteLine(“nCapacity: 0“, dinosaurs.Capacity);/显示List容量 dinosaurs.Add(“Tyrannosaurus“);/向List添加 dinosaurs.Add(“Amargasaurus“); dinosaurs.Add(“Mamenchisaurus“); dinosaurs.Add(“Deinonychus“);,dinosaurs.Add(“Compsognathus“); foreach (string dinosaur in dinosaurs) Conso

16、le.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 (string dinosaur in dinosaurs) Consol

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号