JAVA程序设计第七章教学课件.ppt

上传人:牧羊曲112 文档编号:5436158 上传时间:2023-07-06 格式:PPT 页数:37 大小:226.99KB
返回 下载 相关 举报
JAVA程序设计第七章教学课件.ppt_第1页
第1页 / 共37页
JAVA程序设计第七章教学课件.ppt_第2页
第2页 / 共37页
JAVA程序设计第七章教学课件.ppt_第3页
第3页 / 共37页
JAVA程序设计第七章教学课件.ppt_第4页
第4页 / 共37页
JAVA程序设计第七章教学课件.ppt_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《JAVA程序设计第七章教学课件.ppt》由会员分享,可在线阅读,更多相关《JAVA程序设计第七章教学课件.ppt(37页珍藏版)》请在三一办公上搜索。

1、1,第7章 数组和向量,理解数组的概念学习使用数组的步骤熟悉排序和查找算法使用对象作为数组元素复制数组学习如何使用多维数组熟悉数组包装类和他们的子类了解如何使用命令行参数,2,7.1引言,相同的数据类型元素类型按一定的顺序排列就构成了数组数组元素可以为:基本数据类型某一类的对象建立java数组需要以下三个步骤:声明数组创建数组空间初始化数组元素,3,数 组声明数组,声明数组的语法格式有两种如下:数组元素类型 数组名;数组元素类型数组名;例如:char s;或:char s;int myList;int myList;,4,数 组创建数组空间,例:创建一个由10个double型元素构成的数组do

2、uble myList=new double10,;,5,数 组数组的初始化和处理,说明:数组创建后,它的元素赋予默认值。基本数据类型的数组元素会自动初始化成“空”值(对于数值,空值就是零;对于char,它是null;而对于boolean,它却是false)。数组的大小由arrayObject.length给出数组的元素可以通过下标来访问,下标从0到arrayObject.length-1,6,数 组数组的初始化和处理,说明:创建数组后,可以通过循环语句给这个数组的元素赋值for(int i=0;i myList.length;i+)myListi=(double)i;也可以在创建数组空间的时

3、候,同时将初值给出来,例如:int MyIntArray=1,2,3,4,5,6,7,8,9;存储空间的分配等价于使用new,MyIntArray.length=9,7,数 组创建数组空间,创建一个基本数据类型元素的数组:public char createArray()char s;s=new char 26;for(int i=0;i 26;i+)s i=(char)(A+i);return s;,8,数 组数组的初始化和处理,处理数组元素时,经常使用for循环例7.1划分成绩等级public class AssignGrade/Main method public static void

4、 main(String args)int numOfStudents=0;/The number of students int scores;/Array scores int best=0;/The best score char grade;/The grade/Get number of students System.out.println(Please enter number of students);numOfStudents=MyInput.readInt();,9,数 组数组的初始化和处理,/Create array scores scores=new intnumOfS

5、tudents;/Read scores and find the best score System.out.println(Please enter+numOfStudents+scores);for(int i=0;i best)best=scoresi;,10,数 组数组的初始化和处理,/Assign and display grades for(int i=0;i=best-10)grade=A;else if(scoresi=best-20)grade=B;else if(scoresi=best-30)grade=C;else if(scoresi=best-40)grade=D

6、;else grade=F;System.out.println(Student+i+score is+scoresi+and grade is+grade);,int nums=1,2,3;,11,数 组7.4数组排序,排序算法很多,本节介绍一种简单直观的排序方法,称为选择排序,12,简单选择排序,假设排序过程中,待排记录序列的状态为:,无序序列R1.i-1,有序序列 Ri.n,第 i 趟简单选择排序,从中选出关键字最大的记录,无序序列R1.i,有序序列 Ri+1.n,13,数 组7.4数组排序,排序过程:2 9 5 4 8 1 62 6 5 4 8 1 92 6 5 4 1 8 92 1

7、5 4 6 8 92 1 4 5 6 8 92 1 4 5 6 8 91 2 4 5 6 8 9,14,数 组 7.4数组排序,public class SelectionSort/Main method public static void main(String args)double myList=5.0,4.4,1.9,2.9,3.4,3.5;System.out.println(My list before sort is:);printList(myList);selectionSort(myList);/Print the sorted list System.out.print

8、ln();System.out.println(My list after sort is:);printList(myList);,15,数 组 7.4数组排序,static void printList(double list)for(int i=0;ilist.length;i+)System.out.print(listi+);System.out.println();,16,数 组 7.4数组排序,static void selectionSort(double list)double currentMax;int currentMaxIndex;for(int i=list.len

9、gth-1;i=1;i-)currentMax=listi;currentMaxIndex=i;for(int j=i-1;j=0;j-)if(currentMax listj)currentMax=listj;currentMaxIndex=j;,17,数 组 7.4数组排序,/Swap listi with listcurrentMaxIndex if necessary;if(currentMaxIndex!=i)listcurrentMaxIndex=listi;listi=currentMax;,18,数 组 7.5数组中元素的查找,查找是在数组中寻找特定元素的过程,本节讨论两种算法

10、:线性查找和二分查找。,19,数 组 线性查找法,public class LinearSearchpublic static void main(String args)int list=new int10;System.out.print(The list is);for(int i=0;ilist.length;i+)listi=(int)(Math.random()*100);System.out.print(listi+);System.out.println();System.out.print(Enter a key);int key=MyInput.readInt();int

11、index=linearSearch(key,list);if(index!=-1)System.out.println(The key is found in index+index);else System.out.println(The key is not found in the list);,20,数 组 线性查找法,/The method for finding a key in the list public static int linearSearch(int key,int list)for(int i=0;ilist.length;i+)if(key=listi)ret

12、urn i;return-1;,21,数 组 二分查找法,public class BinarySearch public static void main(String args)int list=new int10;System.out.print(The list is);for(int i=0;ilist.length;i+)listi=2*i+1;System.out.print(listi+);System.out.println();System.out.print(Enter a key);int key=MyInput.readInt();int index=binarySe

13、arch(key,list);if(index!=-1)System.out.println(The key is found in index+index);else System.out.println(The key is not found in the list);,22,数 组 二分查找法,public static int binarySearch(int key,int list)int low=0;int up=list.length-1;return binarySearch(key,list,low,up);public static int binarySearch(i

14、nt key,int list,int low,int up)if(low up)/The list has been exhausted without a match return-1;int mid=(low+up)/2;if(key listmid)return binarySearch(key,list,mid+1,up);return-1;,23,数 组7.6对象的数组,声明并创建一个元素为10个circle对象的数组Circle circleArray=new Circle10;可以用for循环初始化for(int i=0;icircleArray.length;i+)circl

15、eArrayi=new Circle();,24,数 组7.6对象的数组,public class TotalArea public static void main(String args)Circle circleArray;circleArray=createCircleArray();printCircleArray(circleArray);public static Circle createCircleArray()Circle circleArray=new Circle10;for(int i=0;icircleArray.length;i+)circleArrayi=new

16、 Circle(Math.random()*100);return circleArray;,25,数 组7.6对象的数组,public static void printCircleArray(Circle circleArray)System.out.println(The radii of the circles are);for(int i=0;icircleArray.length;i+)System.out.print(tttt+circleArrayi.getRadius()+n);System.out.println(tttt-);/Compute and display th

17、e result System.out.println(The total areas of circles is t+sum(circleArray);,26,数 组7.6对象的数组,/Add circle areas public static double sum(Circle circleArray)/Initialize sum double sum=0;/Add areas to sum for(int i=0;i circleArray.length;i+)sum+=circleArrayi.findArea();return sum;,27,数 组7.7数组的复制,简单的赋值语

18、句并不能完成数组的复制如例7.6public class TestCopyArray public static void main(String args)int list1=0,1,2,3,4,5;int list2=new intlist1.length;list2=list1;System.out.println(Before modifying list1);printList(list1 is,list1);printList(list2 is,list2);for(int i=0;ilist1.length;i+)list1i=0;System.out.println(nAfte

19、r modifying list1);printList(list1 is,list1);printList(list2 is,list2);,28,数 组7.7数组的复制,/The method for printing a list public static void printList(String s,int list)System.out.print(s+);for(int i=0;ilist.length;i+)System.out.print(listi+);System.out.print(n);,29,数 组7.7数组的复制,数组的复制有三种形式:使用循环:int sour

20、ceArray=2,3,1,5,10;int targetArray=new intsourceArray.length;for(int i=0;i sourceArrays.length;i+)targetArrayi=sourceArrayi;,30,数 组7.7数组的复制,使用arraycopyarraycopy(sourceArray,src_pos,targetArray,tar_pos,length);Example:System.arraycopy(sourceArray,0,targetArray,0,sourceArray.length);,31,数 组7.7数组的复制,使用

21、clone方法Int targetArrayi=(int)sourceArray.clone(),32,数 组多维数组,1.定义方式:type 维数arrayName;例如:int intArray;int a2;2.分配内存空间:有两种方法:直接为每一维分配空间,如int a=new int23;int twoDim=new int 4;/error分别为每一维分配空间 如:int a=new int2;a0=new int3;a1=new int3;可以为每行设置为空间大小不同的数组。如:a0=new int3;a1=new int5;,33,数 组多维数组,说明:Java中多维数组被看作

22、数组的数组。例如二维数组为一个特殊的一维数组,其每个元素又是一个一维数组。3.初始化 有两种方式:先定义数组,分配空间,然后直接对每个元素进行赋值 在定义数组的同时进行初始化。如:int a=2,3,1,5,3,4;,34,数 组7.11命令行参数,运行程序时可以从命令行给java程序传递参数class TestMain public static void main(String args).java TestMain arg0 arg1 arg2.argn,35,数 组 7.11命令行参数,在main方法中,参数 args0,args1,.,argsn,对应于命令行中的参数 arg0,ar

23、g1,.,argnArgs.length是传递参数的数量,如:例7.6,36,数 组 7.11命令行参数,public class Calculator/Main method public static void main(String args)/The result of the operation int result=0;if(args.length!=3)System.out.println(Usage:java Calculator operator operand1 operand2);System.exit(0);,37,数 组 7.11命令行参数,switch(args0.

24、charAt(0)case+:result=Integer.parseInt(args1)+Integer.parseInt(args2);break;case-:result=Integer.parseInt(args1)-Integer.parseInt(args2);break;case*:result=Integer.parseInt(args1)*Integer.parseInt(args2);break;case/:result=Integer.parseInt(args1)/Integer.parseInt(args2);System.out.println(args1+args0+args2+=+result);,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号