
ZJWCHC 第九章 2☑172 数组、集合对象和范型
ZJWCHC 第九章 数组、集合对象和范型

回顾 ◆线程是在共享内存空间中并发的多道执行路径 ◆在C#中,是使用System.Threading命名 空间中的Thread类来创建线程的 ◆线程优先级可以更改为ThreadPriority枚举 中定义的值 ◆C#中的Iock关键字是实现线程同步的一种方 法 ◆同步的线程称为安全线程 ◆除非绝对必要,否则不要创建线程安全的代码, 因为添加不必要的锁定会降低性能
2 回顾 线程是在共享内存空间中并发的多道执行路径 在 C# 中,是使用 System.Threading 命名 空间中的 Thread 类来创建线程的 线程优先级可以更改为 ThreadPriority 枚举 中定义的值 C# 中的 lock 关键字是实现线程同步的一种方 法 同步的线程称为安全线程 除非绝对必要,否则不要创建线程安全的代码, 因为添加不必要的锁定会降低性能

目标 ◆使用System.Array对象 ◆理解集合对象的特点和优点 ◆使用System.ArrayList对象 ◆使用哈希表对象
3 目标 使用System.Array 对象 理解集合对象的特点和优点 使用System.ArrayList 对象 使用哈希表对象

System.Array简介3-1 存储学员的分数 数组 int score[new int[7]; Syste Scor 1o] int sc Syste score [1] int sc Syste score [2] int sc Syste score [3] int sc Syste score [4] int sc Syste score [5] int sc 在数组的术语中,元素表示数组中存储的值,数组长度指数组中 存储的值的总数,数组秩指数组的总维数
4 int score1; System.Console.ReadLine(score1) 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) 第七位学生的分数 第六位学生的分数 第五位学生的分数 第四位学生的分数 第三位学生的分数 第二位学生的分数 第一位学生的分数 score [6] score [5] score [4] score [3] score [1] score [0] score [2] System.Array 简介 3-1 应用程序 数 组 存储学员的分数 int score[ ] = new int[7]; 6 7 5 4 3 2 1 在数组的术语中,元素表示数组中存储的值,数组长度指数组中 存储的值的总数,数组秩指数组的总维数

System.Array简介3-2 数组定义:数据类型[]数组名称, (1 22F71. 如何简易地执行对数组的操作? SystemAray MyArray[6] 可以执行各种操作,如存储、检索、排序和反转 MyArray [0]604
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简介3-3 System.Array Array是抽象的基类,提供Createlnstance方法来创建数组 Array obj=Array.Createlnstance(typeof(string),8);
6 System.Array 简介 3-3 System.Array Array是抽象的基类,提供 CreateInstance 方法来创建数组 Array obj = Array.CreateInstance(typeof(string),8);

System.Array的属性和方法 属性 方法 BinarySearch Clear Length Copy CopyTo Rank CreateInstance GetLength IsReadOnly GetLowerBound GetValue IsFixedSize GetUpperBound IndexOf LastlndexOf Reverse SetValue Sort
7 System.Array 的属性和方法 属性 Length 方法 BinarySearch Clear Copy Rank IsReadOnly IsFixedSize CopyTo CreateInstance GetLength GetLowerBound GetUpperBound GetValue IndexOf LastIndexOf Reverse SetValue Sort

示例2-1 static void Main(string[]args) /∥构建obiNames数组 Array objNames Array.Createlnstance(typeof(string),5); /初始化值 将objNames实例化为字符串对象并且其中存放5个元素 obiNames.SetValue,, objNames.SetValue("B",1); obiNames.SetValue("C",2); obiNames.SetValue("D",3); objNames.SetValue("E",4); Console.WriteLine 使用SetValue0方法存储字符串 for(int ctr=0;ctr Console.WriteLine(“元素{o:{1}",ctr+1, objNames.GetValue(ctr)); 使用GetValue0方法检索数组值 8
8 示例 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.SetValue(“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.VriteLine(n数组中元素的总数是 (0)",objNames.Length.ToString()); 显示objNames数组的长度 输出数组秩 Console.WriteLine(n数组秩是{O",obiNames.Rank.ToString( ∥反转数组并输出 显示objNames数组秩 Array.Reverse(obiNames); Console.VriteLine(n反转数组后"): 反转数组元素 for(int ctr=0;ctr 5;ctr++) Console.VriteLine(“元素{o}:{1}",ctr+1, objNames.GetValue(ctr)): 反转后的数组元素列表 课堂练习: 这段代码用Foreach:结构怎么写?
9 示例 2-2 Console.WriteLine(“\n数组中元素的总数是 {0}",objNames.Length.ToString()); //输出数组秩 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,2,3,4,5,6} 和使用: B.Createlnstance(typeof(string),5) 教员演示两种数组的差别 创人, 10
10 思考和演示 使用: int[ ] A = {1,2,3,4,5,6} 和使用: B.CreateInstance (typeof(string),5) 创建的数组,A可以使用B所有的属性和方法吗? 教员演示两种数组的差别