JAVA程序设计第七章教学课件.ppt_第1页
JAVA程序设计第七章教学课件.ppt_第2页
JAVA程序设计第七章教学课件.ppt_第3页
JAVA程序设计第七章教学课件.ppt_第4页
JAVA程序设计第七章教学课件.ppt_第5页
已阅读5页,还剩32页未读 继续免费阅读

下载本文档

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

文档简介

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

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

3、i+) myListi = (double)i; 也可以在创建数组空间的时候,同时将初值给出来,例如: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划

4、分成绩等级 public class AssignGrade / Main method public static void 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); numOfStude

5、nts = MyInput.readInt();,9,数 组数组的初始化和处理,/ Create array scores scores = new intnumOfStudents; / 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)

6、 grade = A; else if (scoresi = best - 20) grade = B; else if (scoresi = best - 30) grade = C; else if (scoresi = best - 40) grade = D; else grade = F; System.out.println(Student + i + score is + scoresi + and grade is + grade); ,int nums = 1, 2,3;,11,数 组7.4数组排序,排序算法很多,本节介绍一种简单直观的排序方法,称为选择排序,12,简单选择排

7、序,假设排序过程中,待排记录序列的状态为:,无序序列R1.i-1,有序序列 Ri.n,第 i 趟 简单选择排序,从中选出 关键字最大的记录,无序序列R1.i,有序序列 Ri+1.n,13,数 组7.4数组排序,排序过程: 2 9 5 4 8 1 6 2 6 5 4 8 1 9 2 6 5 4 1 8 9 2 1 5 4 6 8 9 2 1 4 5 6 8 9 2 1 4 5 6 8 9 1 2 4 5 6 8 9,14,数 组 7.4数组排序,public class SelectionSort / Main method public static void main(String args

8、) 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.println(); System.out.println(My list after sort is: ); printList(myList); ,15,数 组 7.4数组排序,static void printList(double list) for

9、 (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.length-1; i=1; i-) currentMax = listi; currentMaxIndex = i; for (int j=i-1; j=0; j-) if (currentMax listj) cur

10、rentMax = listj; currentMaxIndex = j; ,17,数 组 7.4数组排序,/ Swap listi with listcurrentMaxIndex if necessary; if (currentMaxIndex != i) listcurrentMaxIndex = listi; listi = currentMax; ,18,数 组 7.5数组中元素的查找,查找是在数组中寻找特定元素的过程,本节讨论两种算法: 线性查找和二分查找。,19,数 组 线性查找法,public class LinearSearch public static void mai

11、n(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 index = linearSearch(key, list); if (index != -1) S

12、ystem.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) return i; return -1; ,21,数 组 二分查

13、找法,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

14、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 s

15、tatic int binarySearch(int 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; 可

16、以用for循环初始化 for (int i=0; icircleArray.length; i+) circleArrayi = 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

17、 = new Circle10; for (int i=0; icircleArray.length; i+) circleArrayi = new 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(t

18、ttt + circleArrayi.getRadius() + n); System.out.println(tttt-); / Compute and display the 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

19、for (int i = 0; i circleArray.length; i+) sum += circleArrayi.findArea(); return sum; ,27,数 组7.7数组的复制,简单的赋值语句并不能完成数组的复制如例7.6 public 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 modifyi

20、ng list1); printList(list1 is , list1); printList(list2 is , list2); for (int i=0; ilist1.length; i+) list1i = 0; System.out.println(nAfter modifying list1); printList(list1 is , list1); printList(list2 is , list2); ,28,数 组7.7数组的复制,/ The method for printing a list public static void printList(String

21、 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 sourceArray = 2, 3, 1, 5, 10; int targetArray = new intsourceArray.length; for (int i = 0; i sourceArrays.length; i+) targetArrayi = sourceArra

22、yi;,30,数 组7.7数组的复制,使用arraycopy arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);,31,数 组7.7数组的复制,使用clone方法 Int targetArrayi = (int )sourceArray.clone(),32,数 组多维数组,1. 定义方式:type 维数arrayName; 例如:int intArray;int

23、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中多维数组被看作数组的数组。例如二维数组为一个特殊的一维数组,其每个元素又是一个一维数组。 3. 初始化 有两种方式: 先定义数组,分配空间,然后直接对每个元素进行赋值 在定义数组的同时进行初始化。 如:int a = 2,3, 1,5, 3,4;,34,数 组7.11命令行参数,运行程序时可以从命令行给java程序传递参数 class TestMain public static void main(String args) . java Test

温馨提示

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

评论

0/150

提交评论