Presentation is loading. Please wait.

Presentation is loading. Please wait.

第2章回顾 标识符:不用记,动手 关键字:if, else, switch, for, while, do, break, continue, void, …… 局部变量和成员变量 ①变量作用域 ②内存布局 基本数据类型 ①4类8种 ②互相转换 流程控制语句 ①分支 if……else, switch.

Similar presentations


Presentation on theme: "第2章回顾 标识符:不用记,动手 关键字:if, else, switch, for, while, do, break, continue, void, …… 局部变量和成员变量 ①变量作用域 ②内存布局 基本数据类型 ①4类8种 ②互相转换 流程控制语句 ①分支 if……else, switch."— Presentation transcript:

1 第2章回顾 标识符:不用记,动手 关键字:if, else, switch, for, while, do, break, continue, void, …… 局部变量和成员变量 ①变量作用域 ②内存布局 基本数据类型 ①4类8种 ②互相转换 流程控制语句 ①分支 if……else, switch ②循环 for,while,do……while

2 第3章 方法与数组 3.1 方法 1.定义 2.格式 3.调用 4.return语句 5.方法重载 6.递归调用 3.2 数组 1.一维数组
⑴创建数组 ⑵访问数组元素 ⑶数组长度 ⑷数组初始化 2.一维数组应用 3.二维数组

3 main()执行后,可以完成判断一个给定的整数是奇数 还是偶数的功能。
public class Demo02 { public static void main(String[] args) { int x = 3; if (x % 2 == 0) { System.out.println(x + "是偶数") ; } else { System.out.println(x + "是奇数") ; } main()执行后,可以完成判断一个给定的整数是奇数 还是偶数的功能。

4 方法 定义:方法就是一段用来完成特定功能,可重复使用的代码段。类似其他语言中的函数。 格式:
[修饰符] 返回值类型 方法名(形式参数列表) { 方法体 } public class HelloWorld { public static void main(String[ ] args) { System.out.println("Hello World!"); }

5 方法里的概念 [修饰符] 返回值类型 方法名(形式参数列表){ 语句;…… } 形式参数:方法被调用时,用于接收外界输入的数据
实参:调用方法时,实际传给被调用的方法的数据。 返回值:被调的方法执行完后,返还给调用处的数据 返回值类型:事先约定的返还值类型。无返回值void 方法命名规范要求 第1个单词的首字母小写,之后每个单词的首字母大写如:printInfo()

6 方法调用 调用方法格式:对象名.方法名(实参列表) 要求:实参的数目、数据类型和次序必须与所调用的方法的形参列表相匹配。
传递原则:传递参数时遵循值传递原则。 ①基本数据类型传递的是值的本身。 ②引用数据类型传递的是对象的引用,而不是 对象本身。 return语句:①终止方法运行,返回该调用处 ②同时也可返回结果 TestMethod.java

7 方法重载 方法重载就是方法名称相同,但参数的类型和参数的个数不同。 作用:在一个类里,解决方法同名问题。
例:MethodDemo02.java 通过传递参数的类型及个数不同,编译器可以自动完成不同功能的方法调用。 注意:方法的重载跟返回类型没关。

8

9 递归调用 递归调用就是在一个方法的内部对方法的自身进行的调用。 注意:递归调用在操作时如果处理不好,有可能出现内存的溢出,所以谨慎使用。

10 //求5! public class MethodDemo03 { public static void main(String[] args) { System.out.println( method(5) ); } public static int method( int n ) { if (n == 1) { return 1; } else { return n * method(n-1) ;

11 返回值 method( 3 ) return 3*2*1; method( 2 ) return 2*1;
…… method( 3 ) if (n == 1) return 1; else return 3*method(2) ; return 2*method(1) ; public static int method( int n ) { if (n == 1) { return 1; } else { return n * method(n-1) ; } 返回值 method( 3 ) return 3*2*1; method( 2 ) return 2*1; method( 1 ) return 1;

12 main method(5) method(4) method(1) method(3) method(2)

13 一维数组 数组用来存放一组相同数据类型的数据的集合 Java中的数组属于引用数据类型。 定义数组必须经过两个步骤: (1) 声明数组
数据类型[ ] 数组名 = null; (2) 分配内存空间给数组 数组名 = new 数据类型[长度]; int[] a; float b[]; char[] c; double[] d; boolean e[]; long f[];

14 声明数组 int[] score = null;
内存情况 声明数组 int[] score = null; heap(堆) stack(栈) data segment code segment new出来的东西 局部变量 静态变量 字符串常量 存放代码 说明:声明数组后,编译器仅会在栈内存中分配一块内存空间存此数组的名字,score还没有指向具体的空间,所以无法使用。

15 内存情况 (2)为数组开辟空间 score = new int[3];
创建数组之后,编译器会在栈内存放数组名,在堆内存放的数组元素。 引用实质就是指向,有指针的含义。 栈内存放的东西实质是数组元素的首地址。 new关键字作用:命令编译器根据括号里的长度在堆内存中开辟一块内存给该数组使用。

16 一维数组 访问数组中某元素的方法格式:数组名[下标] 求数组长度: 数组名.length
注意:⑴数组被创建后,其大小不能被改变,但数组中的各个数组元素的值是可以被改变的。 ⑵访问数组中的元素时,下标索引不能越界, 范围必须在0 ~ length-1 例: int[] score = new int[10] ; score[0](第1个元素) score[1] score[9](最后一个元素)

17 数组初始化 ①静态初始化:在声明的同时指定具体内容 int[] a = { 52, 63, 85, 98, 14, 88 } ;
②动态初始化:将数组的定义和数组的初始 化分开来进行。 例1: int[] array = new int[2] ; array[0]=1; array[1]=2; 例2: int[] array=new int[10]; for(int i=0;i<10;i++){ array[i]=i+2; }

18 数组应用:为数组中的元素赋值并输出 public class ArrayDemo01 {
public static void main(String[] args) { int score[] = new int[3]; for (int i = 0; i < score.length; i ++) { score[i] = 2*i+1; } for (int i = 0; i < score.length ; i ++){ System.out.println("score["+i+"]="+score[i]);

19 数组应用:求数组中元素的最大、小值 int a[] = {10, 3, -1, 119, -51, 100, 56, 90}; 思路分析
1.静态初始化数组 2.定义2个整型变量max和min,存放最值 3.比较⑴将第1个元素分别赋给max和min ⑵逐一比较(for语句、if语句) ⑶循环次数用 数组.length 来求 4.打印输出 min 10 3 -1 119 -51 100 56 90 max

20 public class ArrayDemo02{
public static void main(String[] args) { int a[] = {10, 3, -1, 119, -51, 100, 56, 90}; int max = a[0]; int min = a[0]; for (int i = 1; i < a.length; i ++) { if ( a[i] > max ) { max = a[i]; } if ( a[i] < min ) { min = a[i]; System.out.println("MXA="+ max); System.out.println("MIN=" + min);

21 数组应用:使用循环语句进行数组复制 public class ArrayCopyFor {
public static void main(String[] args) { int[] a1 = {1, 2, 3, 4, 5}; int[] a2 = new int[ a1.length ] ; for (int i = 0; i < a1.length; i ++){ //复制 a2[i]=a1[i]; } for (int i = 0; i < a2.length; i ++){ //输出 System.out.println(a2[i]);

22 使用System.arraycopy()复制数组
System.arraycopy( from, fromIndex, to, toIndex, count) public class ArrayCopy{ public static void main(String[] args) { int[] a1 = {1,2,3,4,5}; int[] a2 = new int[a1.length]; //复制 System.arraycopy(a1, 0, a2, 0, a1.length); //输出 for(int i=0;i<a2.length;i++){ System.out.println(a2[i]); } ArrayCopy01.java

23 方法调用 传递参数原则: ①基本数据类型传递的是值的本身。 ②引用数据类型传递的是对象的引用,不是对象本身.
public class MethodDemo01 { public static void main(String args[]) { int one = addOne(29, 58) ; //调用addOne()方法 } public static int addOne(int x, int y){ //定义方法 int temp = 0; temp = x + y; return temp; //返回计算结果

24 数组的引用传递 向方法中传递数组. //读程序,写结果 public class ArrayRefDemo01 {
数组的引用传递 向方法中传递数组. //读程序,写结果 public class ArrayRefDemo01 { public static void main(String args[]) { int temp[] = {1, 3, 5}; //静态初始化 fun (temp) ; //传递数组 for (int i=0; i<temp.length; i++){ System.out.println(temp[i]); } public static void fun (int[] x) { //接收数组 x[0] = 6; 分析内存情况

25 数组应用:排序 (从小到大) 算法就是在有限的步骤内求解某一个问题所使用的一组定义明确的规则。 算法的好坏直接影响程序的运行效率。
int[] arr={16, 5, 8, 2, -6, 0, 10}; 算法就是在有限的步骤内求解某一个问题所使用的一组定义明确的规则。 算法的好坏直接影响程序的运行效率。 排序算法:选择、冒泡、希尔、快速、…… 思路: 例:SelectionSort.java 例:BubbleSort.java

26 a0 a1 a2 a3 a4 a5 a6 16 5 8 2 -6 10 外层 内层 比较 交换 i = 0 j = 1 a1 < a0
10 外层 内层 比较 交换 i = 0 j = 1 a1 < a0 j = 2 a2 < a0 j = 3 a3 < a0 j = 4 a4 < a0 j = 5 a5 < a0 j = 6 a6 < a0 i = 1 a2 < a1 5 16 8 2 -6 10 2 16 8 5 -6 10 -6 16 8 5 2 10 外层循环1次,就能选出一 个最小的数放在最前面。 -6 8 16 5 2 10 SelectionSortPrint.java

27 a0 a1 a2 a3 a4 a5 a6 16 5 8 2 -6 10 外层 内层 比较 交换 i = 0 j = 0 a0 > a1
10 外层 内层 比较 交换 i = 0 j = 0 a0 > a1 j = 1 a1 > a2 j = 2 a2 > a3 j = 3 a3 > a4 j = 4 a4 > a5 j = 5 a5 > a6 i = 1 5 16 8 2 -6 10 5 8 16 2 -6 10 5 8 2 16 -6 10 5 8 2 -6 16 10 5 8 2 -6 16 10 5 8 2 -6 10 16 外层循环1次,将最大的数放到了最后 5 8 2 -6 10 16

28 数组排序:直接使用Java类库中的方法 import java.util.Arrays ; public class ArraySort {
public static void main(String[] args) { int[] a = { 16, 5, 8, 92, -66, 0, 100, 45}; Arrays.sort(a); //排序 for(int i=0;i<a.length;i++){ //打印 System.out.print(a[i]+","); }

29 二维数组 格式: 例: int[][] a = new int[3][4]; 初始化:动态、静态。
1 2 3 10 -1 119 -51 100 56 90 49

30 int[][] a={{10,3},{-1,119,-51},{100,56,90,49}};
heap 10 3 -1 119 -51 stack a[0] a[1] a[2] 100 56 90 49 a

31 //二维数组静态初始化 public class Array2DDemo { public static void main(String[] args) { int[][] a = {{10,3},{-1,119,-51},{100,56,90,49}}; for (int i = 0; i < a.length; i ++) { for(int j = 0; j < a[i].length; j ++){ System.out.print(a[i][j]+"\t"); } System.out.println("");

32 使用数组结构实现杨辉三角的存储和打印 分析:
杨辉三角的规律是:它的一条斜边和一条垂直边是由数字1组成,其余的数字等于它上方和左上方的两个数之和。 假设元素为整型。

33 public class YangHuiTriangle {
public static void main(String[] args){ int row = 10; //行数 int[][] array = new int[row][]; //二维数组存储三角数字 for (int i = 0; i < row; i ++){ //初始化三角 array[i] = new int[i + 1]; array[i][0] = 1; //两条边为1 array[i][i] = 1; } for(int i = 2; i < row; i ++){ //其它位置的数 for(int j = 1; j < i; j ++){ array[i][j] = array[i - 1][j - 1] + array[i - 1][j]; for(int i = 0; i < row; i ++){ //打印杨辉三角 for(int j = 0; j <= i; j ++){ System.out.print(array[i][j]+"\t"); System.out.println("");

34 数据输入 Java提供专门输入数据类Scanner,在java.util包中 //接收String类的数据
import java.util.*; public class ScannerStringDemo { public static void main(String[] args) { //从键盘输入数据 Scanner scan = new Scanner(System.in); System.out.println("请输入数据"); String str = scan.next(); System.out.println("输入的数据是:" + str); }

35 数据输入 Java提供专门输入数据类Scanner,在java.util包中 //接收整型类的数据 import java.util.*;
public class ScannerStringDemo { public static void main(String[] args) { //从键盘输入数据 Scanner scan = new Scanner(System.in); System.out.println("请输入数据"); int i = scan.nextInt(); System.out.println("输入的数据是:" + i); }

36 数据输入 Java提供专门输入数据类Scanner,在java.util包中 //接收浮点型类的数据 import java.util.*;
public class ScannerStringDemo { public static void main(String[] args) { //从键盘输入数据 Scanner scan = new Scanner(System.in); System.out.println("请输入数据"); float f = scan.nextFloat(); System.out.println("输入的数据是:" + f); }


Download ppt "第2章回顾 标识符:不用记,动手 关键字:if, else, switch, for, while, do, break, continue, void, …… 局部变量和成员变量 ①变量作用域 ②内存布局 基本数据类型 ①4类8种 ②互相转换 流程控制语句 ①分支 if……else, switch."

Similar presentations


Ads by Google