数组与集合实用教案_第1页
数组与集合实用教案_第2页
数组与集合实用教案_第3页
数组与集合实用教案_第4页
数组与集合实用教案_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、16.1 一维数组 6.1.1 一维数组的创建及初始化 一维数组的声明与分配语句的语法(yf)形式: 数据类型 数组名 = new 数据类型符长度; 或者: 数据类型 数组名; 数组名=new 数据类型符长度; 示例: double score=new double300 ; 或者: double score; score=new double 300 ; 第1页/共40页第一页,共40页。26.1.1 一维数组的创建(chungjin)及初始化 为数组元素赋初值的语法形式(xngsh): 数据类型符 数组名 = 初值列表; 或者: 数据类型符 数组名= new 数据类型符 初值列表; 示例:

2、 int x=1,2,3,4; int x=new int 1,2,3,4;第2页/共40页第二页,共40页。36.1.2 一维数组的简单( jindn)应用示例(shl):double aver = 0.0;double score=new double300 ;for(int i=0;i300;i+) scorei=i;for (int j = 0; j 300; j+) aver = aver + score j; aver = aver / 300;第3页/共40页第三页,共40页。46.2 二维数组 6.2.1 二维数组的创建及初始化 二维数组的定义(dngy)与分配语法形式: 数据

3、类型符 , 数组名 =new数据类型符长度1,长度2; 或者 数据类型符 , 数组名; 数组名=new 数据类型符 长度1,长度2; 示例: int , a=new int 3,4; 或者 int , a; a=new int 3,4; /为数组分配存储空间 第4页/共40页第四页,共40页。56.2.1 二维数组的创建(chungjin)及初始化 为数组元素赋初值的语法形式: 数据类型符 , 数组名 =new int , 初始(ch sh)列表1,初始(ch sh)列表2, ,初始(ch sh)列表n ; 或者 数据类型符 , 数组名 =初始(ch sh)列表1,初始(ch sh)列表2,初

4、始(ch sh)列表n ; 示例: int,a=new int ,1,2,3,4,5,6; 或者: int,a=1,2,3,4,5,6;第5页/共40页第五页,共40页。66.2.2 二维数组的简单( jindn)应用示例:某班有M名同学,本学期开了N门课,期末考试后,要统计每个学生的平均分,请编写程序代码实现该功能,要求对于每个学生要输入学号和N门课的成绩(chngj)。static void Main(string args) const int M = 2; const int N = 2; int, score= new intM, N + 1; int i, j; double av

5、er = new doubleM; for (i = 0; i M; i+) Console.WriteLine(请输入第0个人的学号和成绩(chngj):,i+1); score i , 0 = Convert.ToInt32(Console.ReadLine(); for ( j=1;j=N ;j+) score i, j = Convert.ToInt32(Console.ReadLine(); 第6页/共40页第六页,共40页。76.2.2 二维数组的简单( jindn)应用接上页for (i = 0; i M; i+) averi = 0; for ( j = 1; j = N; j

6、+) averi = averi + score i, j; averi = averi /N ; for (i = 0; i M; i+) Console.WriteLine(); for ( j=0;j=N ;j+) Console .Write (0 , score i,j ); Console.Write(0 ,aver i ); Console.Read(); 第7页/共40页第七页,共40页。86.3 交错(jiocu)数组第8页/共40页第八页,共40页。96.3.1 交错( jiocu)数组的创建及初始化分配行:数据类型符 数组名=new 数据类型符 行数 ;示例:int b=

7、new int 3 ; /定义具有( jyu)3行的交错数组各行数组元素个数的分配 :数组名i=new 数据类型符 长度; b0=new int 2;/首行具有( jyu)两个元素 b1=new int 3; /第二行具有( jyu)3个元素 b2=new int 4; /第三具有( jyu)有4个元素第9页/共40页第九页,共40页。106.3.1 交错( jiocu)数组的创建及初始化交错数组可以(ky)在声明时进行初始化。示例:int a=new int new int 1,2, new int 3,4,5, new int 6,7,8,9;或者: int a= new int 1,2,

8、 new int 3,4,5, new int 6,7,8,9 ;第10页/共40页第十页,共40页。116.3.2 交错( jiocu)数组的简单应用 示例: 编程输出杨辉三角的前五行(wxng)。杨辉三角的前五行(wxng)值如下。 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1第11页/共40页第十一页,共40页。126.3.2 交错( jiocu)数组的简单应用static void Main(string args) const int M = 5; int yhsj = new intM ; int i, j; for (i = 0; i 5; i+) yhsji =

9、new inti + 1; for (i = 0; i M; i+) yhsji0 = 1; yhsjii = 1; for (i = 2; i 5; i+) for ( j = 1; j i; j+) yhsji j = yhsji - 1 j - 1 + yhsji - 1 j; for (i = 0; i M; i+) Console.WriteLine(); for ( j=0;j=i;j+) Console .Write(0 ,yhsj i j ); 第12页/共40页第十二页,共40页。136.4 遍历(bin l)数组6.4.1 使用(shyng)for循环遍历 在遍历数组时,最

10、常用的3种方式就是使用(shyng)Array类提供的Length属性、GetUpperBound方法和GetLength方法。遍历一维数组 示例:int a = 1, 2, 3, 4, 5, 6 ;for (int i = 0; i a.Length; i+) Console.Write(0,-4, ai);Console.Read();第13页/共40页第十三页,共40页。146.4.1 使用(shyng)for循环遍历示例(shl):通过GetUpperBound方法获取数组的最高下标。int a = 1, 2, 3, 4, 5, 6 ;for (int i = 0; i a.GetUp

11、perBound(0)+1; i+) Console.Write(0,-4, ai);Console.Read();注: GetUpperBound方法中的参数0表示0维,一维数组的维数就是0。第14页/共40页第十四页,共40页。156.4.1 使用(shyng)for循环遍历遍历(bin l)二维数组示例:int, a = 1, 2, 3, 4, 5, 6 ;int n = a.Length;Console.WriteLine(一共有0个元素!,n );for (int i = 0; i a.GetLength(0)+1; i+) Console.WriteLine(); for (int

12、 j = 0; j a.GetLength(1)+1;j+ ) Console.Write(0,-4, ai,j ); Console.Read();第15页/共40页第十五页,共40页。166.4.1 使用for循环(xnhun)遍历遍历交错数组 示例(shl):int a = new int 2 ;a0=new int 1,2,3;a1=new int 4,5;int n = a.Length;Console.WriteLine(一共有0个元素!,n );for (int i = 0; i a.Length; i+) Console.WriteLine(); for (int j = 0;

13、 j a i .Length ; j+) Console.Write(0,-4, ai j);Console.Read();第16页/共40页第十六页,共40页。17 注意Array类的Length属性在二维数组和交错( jiocu)数组中的用法区别。第17页/共40页第十七页,共40页。186.4.2 使用(shyng)foreach遍历一维数组示例(shl):int numbers = 4, 5, 6, 1, 2, 3, -1, -2, 0 ;foreach (int i in numbers) Console.WriteLine(i);第18页/共40页第十八页,共40页。196.4.2

14、 使用(shyng)foreach遍历二维数组示例(shl):int , numbers =new int3,2 4,44 ,5,55 ,6,66;foreach (int i in numbers) Console.Write(0,-3 , i );Console.Read();第19页/共40页第十九页,共40页。206.5 常用(chn yn)集合 6.5.1 ArrayList类 ArrayList是一个集合,使用方法与Array类似(li s),但Array是数组,有维数且大小固定,而ArrayList没有维数,大小不固定,故ArrayList又称为动态数组。 常用属性: Count

15、:获取动态数组中实际所包含的元素数 Item:获取或设置指定索引处的元素 Capacity:获取动态数组的容量大小第20页/共40页第二十页,共40页。216.5.1 ArrayList类常用(chn yn)方法:Add、Remove、RemoveAt、Clear、Insert、IndexOf、Reverse、Sort、ToArray、Contains。示例: ArrayList li = new ArrayList(8);注意,这里的8指容量,容量与元素的个数是两个完全不同的概念,集合中的元素个数通过属性Count获取,而容量则是通过Capacity属性获取。容量总是大于或等于元素的个数。

16、第21页/共40页第二十一页,共40页。226.5.1 ArrayList类示例(shl):static void Main(string args) ArrayList Li = new ArrayList(2); Console.WriteLine(Li中的元素的个数为0, Li.Count); Console.WriteLine(Li中的容量为0, Li.Capacity ); int arr = new int 10; Console.WriteLine(arr中的元素的个数为0,arr.Length ); Console.Read();第22页/共40页第二十二页,共40页。23添加

17、(tin ji)元素 Add方法,添加元素,自动添加到列表的末尾(mwi)。示例:static void Main(string args) ArrayList Li = new ArrayList (); Li.Add(a); Li.Add(nihao ); Li.Add(1); for (int i = 0; i Li.Count; i+) Console.Write(0,-4,Lii ); Console.Read(); 第23页/共40页第二十三页,共40页。24插入(ch r)元素ArrayList提供了一个用于插入操作的方法Insert,该函数的语法形式如下(rxi): publi

18、c void Insert( int index, Object value)示例:ArrayList Li = new ArrayList();string arr = new string wo, men, shi, peng, you ;Li.Add(a);Li.Add(nihao );Li.Add(1);Li.AddRange(arr); /注意与Li.Add (arr)的区别; Li.Add(21);Console.WriteLine (Li.Count);Console.WriteLine(Li.Capacity);Console.WriteLine(排序前的顺序为:);第24页/

19、共40页第二十四页,共40页。25插入(ch r)元素 代码接上页 for (int i = 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i+1) % 5 = 0) Console.WriteLine(); Li.Insert(0,hehe); Console.WriteLine(); Console.WriteLine(插入(ch r)后的顺序为:); for (int i = 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i +1)% 5 = 0) Console.WriteLin

20、e(); Console.Read();第25页/共40页第二十五页,共40页。26删除(shnch)元素对于ArrayList类的实例不再(b zi)需要的元素,可以通过Remove和RemoveAt删除。示例:static void Main(string args) ArrayList Li = new ArrayList(); string arr = new string wo, men, shi, peng, nihao ; Li.Add(a); Li.Add(nihao); Li.Add(1); Li.AddRange(arr); Li.Add(21); Console.Writ

21、eLine(排序前的顺序为:);第26页/共40页第二十六页,共40页。27删除(shnch)元素代码接上页for (int i = 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i+1) % 5 = 0) Console.WriteLine(); Li.Remove(nihao); Console.WriteLine(); Console.WriteLine(排序(pi x)后的顺序为:); for (int i = 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i + 1) % 5

22、= 0) Console.WriteLine(); Console.Read();注意:Remove方法负责(fz)移除ArrayList对象的第一个匹配项RemoveAt的参数不是要删除的元素,而是其下标,例如RemoveAt(1)第27页/共40页第二十七页,共40页。28元素(yun s)的排序Sort方法用于排序(pi x)。示例:static void Main(string args) ArrayList Li = new ArrayList(); string arr = new string wo, men, shi, peng, you ; Li.Add(a); Li.Add

23、(nihao); Li.Add(1); Li.AddRange(arr); Li.Add(21); Console.WriteLine(排序(pi x)前的顺序为:);第28页/共40页第二十八页,共40页。29元素(yun s)排序 代码(di m)接上页 for (int i = 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i+1) % 5 = 0) Console.WriteLine(); Li.Sort(); Console.WriteLine(); Console.WriteLine(排序后的顺序为:); for (int i

24、= 0; i Li.Count; i+) Console.Write(0,-8 , Lii); if (i + 1) % 5 = 0) Console.WriteLine(); Console.Read(); 注意:元素类型必须相同(xin tn),否则排序会出错!第29页/共40页第二十九页,共40页。306.5.2 哈希表 哈希表,是按照链表的形式进行存储的,不需要顺序排列,而ArrayList类是按照顺序进行排列的。对于链表存储的结构,需要通过(tnggu)索引进行访问。第30页/共40页第三十页,共40页。31 Hashtable类表示哈希表,是一个键/值对的集合。 常用(chn yn

25、)属性:Keys,Values ,Count。 常用(chn yn)方法:Add、Remove、Clear、 ContainKey、ContainValue 第31页/共40页第三十一页,共40页。326.5.2 哈希表示例:static void Main(string args) Hashtable ht = new Hashtable(); ht.Add(001,number); ht.Add (002,grade); ht.Add(003, name); Console.WriteLine(输出ht的键/值的对数(du sh)为:0, ht.Count ); Console.Write

26、Line(请输入要查找的键:); string a = Console.ReadLine(); if (ht.ContainsKey(a) Console.WriteLine(找到键0所对应的值1, a, hta); else Console.WriteLine(在ht中没有找到要查找的键0,a); ht.Remove(003);第32页/共40页第三十二页,共40页。336.5.2 哈希表 Console.WriteLine(请输入要查找的键:); string b = Console.ReadLine(); Console.WriteLine(“键0所对应(duyng)的值1在哈希表中”,

27、 b, ht.ContainsKey(b)?”包含”:”没有包含”); String value=“name”; Console.WriteLine(“值0所对应(duyng)的键1在哈希表中”, value, ht.ContainsValue(value)?”包含”:”没有包含”); Console.WriteLine(“在没有使用方法clear前输出ht的键/值的对数为:0” ,ht.Count); ht.Clear(); Console.WriteLine(“使用了clear方法后,输出的键/值的对数为:0”,ht.Count); Console.ReadLine();第33页/共40页

28、第三十三页,共40页。34遍历(bin l)哈希表 遍历哈希表通过foreach语句来实现,有两种方式(fngsh)。一种是使用DictionaryEntry结构体访问Hashtable中的元素;另一种就是使用普通的类型作为只读变量,通过键的下标来访问。 第34页/共40页第三十四页,共40页。35遍历(bin l)哈希表使用DirectonaryEntry遍历哈希表 示例(shl):static void Main(string args) Hashtable ht = new Hashtable(); ht.Add(001,number); ht.Add (002,grade); ht.A

29、dd(003, name); ht.Add(004,age); ht.Add(005, sex); Console.WriteLine(输出ht的键/值的对数为:0,ht.Count ); foreach (DictionaryEntry de in ht) Console.Write(0,-4,de.Key); Console.Write(0,-4,de.Value); Console.WriteLine(); Console .ReadLine ();第35页/共40页第三十五页,共40页。36遍历(bin l)哈希表通过键的下标遍历(bin l)哈希表示例:static void Main(string args) Hashtable sh = new Hashtable(); sh.Add(001, str1); sh.Add(002, str2); sh.Add(003, str3); Console.WriteLine(code string ); foreach (string s in sh.Keys) Console.Writ

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论