一,方法(函数)
1:函数的概念?函数的格式?格式的解释说明
概念:完成特定功能的代码块;
格式:
修饰符 返回值 方法名(参数类型 参数1,参数类型 参数2,参数类型 参数3......){
方法体语句;
return 返回值;
}
说明:
修饰符:目前就用public static
返回值类型:就是功能结果的数据类型
方法名:需符合命名规范
形式参数:方法所定义的,用于接收实际参数
实际参数:实际参与运算的参数
参数类型:参数的数据类型
参数名:变量名
方法体语句:完成功能的代码
return:结束方法的语句
返回值:功能的结果,由return带给调用者,返回值是什么类型,返回值类型就是该类型
2:函数的调用
A:明确返回值类型的函数调用
(1)单独调用:一般来说没有意义,所以不推荐
(2)输出调用:由于可能需要针对结果进行进一步的操作,所以不够好
(3)赋值调用:推荐方案
B:void类型的函数调用
用于接收无具体返回值类型的方法,只能单独调用,不能输出调用或赋值调用.
3:函数的练习:
A:求两个数据之和
1 import java.util.Scanner; 2 class Sum { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入第一个数"); 6 int a = sc.nextInt(); 7 System.out.println("输入第二个数"); 8 int b = sc.nextInt(); 9 10 int sum = add(a ,b); 11 12 System.out.println(a + "+" + b + "=" + sum); 13 } 14 15 public static int add(int a,int b){ 16 return a + b; 17 } 18 }
B:判断两个数据是否相等
1 import java.util.Scanner; 2 class Equal { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入第一个数"); 6 int a = sc.nextInt(); 7 System.out.println("输入第二个数"); 8 int b = sc.nextInt(); 9 10 System.out.println(equal(a ,b)); 11 } 12 13 public static boolean equal(int a,int b){ 14 return a == b; 15 } 16 }
C:获取两个数中较大的值
1 import java.util.Scanner; 2 class Max { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入第一个数"); 6 int a = sc.nextInt(); 7 System.out.println("输入第二个数"); 8 int b = sc.nextInt(); 9 10 System.out.println(getMax(a ,b) + " is larger."); 11 } 12 13 public static int getMax(int a,int b){ 14 return a > b ? a : b; 15 } 16 }
D:打印m行n列的星形矩形
1 import java.util.Scanner; 2 class PrintStar { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入行数"); 6 int a = sc.nextInt(); 7 System.out.println("输入列数"); 8 int b = sc.nextInt(); 9 10 printStar(a ,b); 11 } 12 13 public static void printStar(int a,int b){ 14 for (int i = 1;i <= a ;i++ ) { 15 for (int j = 1;j <= b ;j++ ) { 16 System.out.print('*'); 17 } 18 System.out.println(); 19 } 20 } 21 }
E:打印nn乘法表
1 import java.util.Scanner; 2 class MultiplicationTable{ 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入一个整数"); 6 int a = sc.nextInt(); 7 8 makeMultiplicationTable(a); 9 } 10 11 public static void makeMultiplicationTable(int a){ 12 for (int i = 1;i <= a ;i++ ) { 13 for (int j = 1;j <= i ;j++ ) { 14 System.out.print(i + "*" + j + "=" + i * j + "\t"); 15 } 16 System.out.println(); 17 } 18 } 19 }
4:什么是函数重载?以及函数重载的练习?把讲过的案例练习一次即可
函数重载即是函数名相同,而函数参数个数或参数数据类型不同的情况下,通过参数个数和参数数据类型来区分不同函数.
例子:
1 class Sum { 2 public static void main(String[] args) { 3 int a = 10, b = 20, c = 30; 4 double d = 30, e = 40; 5 6 System.out.println(add(a ,b)); 7 System.out.println(add(a ,b ,c)); 8 System.out.println(add(d ,e)); 9 System.out.println(add(d ,a)); 10 System.out.println(add(b ,e)); 11 } 12 13 public static int add(int a,int b){ 14 return a + b; 15 } 16 17 public static int add(int a,int b,int c){ 18 return a + b + c; 19 } 20 21 public static double add(double a,double b){ 22 return a + b; 23 } 24 25 public static double add(double a,int b){ 26 return a + b; 27 } 28 29 public static double add(int a,double b){ 30 return a + b; 31 } 32 }
===============================================================
===============================================================
二. 内存图
画图操作:
1.一个数组的内存图
2.两个数组的内存图
3.三个引用两个数组的内存图
=======================================
=======================================
三.数组
1:数组的概念?有什么特点?
数组是储存同类型多个元素的集合;
数组既可以储存基本数据类型,也可以储存引用数据类型.
2:一维数组的定义格式?
数据类型[] 数组名 = new 数据类型[数组长度];
3:数组操作的两个小问题
ArrayIndexOutOfBoundsException:数据索引越界异常
原因:访问了不存在的索引.
NullPointerException:空指针异常
原因:数组已不再指向堆内存了,而你还用数组名访问元素.
4:数组常见操作:
数组遍历(依次输出数组中的每一个元素)
1 class PrintArray { 2 public static void main(String[] args) { 3 int[] arr = {23 ,45 ,89 ,289 ,12 ,974 ,3 ,64 ,82}; 4 5 printArray(arr); 6 } 7 8 public static void printArray(int[] arr){ 9 for (int i = 0;i < arr.length ;i++ ) { 10 System.out.print(arr[i] + " "); 11 } 12 } 13 }
数组获取最值(获取数组中的最大值最小值)
1 class GetMax { 2 public static void main(String[] args) { 3 int[] arr = {23 ,45 ,89 ,289 ,12 ,974 ,3 ,64 ,82}; 4 5 System.out.println(getMax(arr)); 6 } 7 8 public static int getMax(int[] arr){ 9 int temp = arr[0]; 10 for (int i = 0;i < arr.length ;i++ ) { 11 temp = temp > arr[i] ? temp : arr[i]; 12 } 13 return temp; 14 } 15 }
数组元素逆序 (就是把元素对调)
1 class Reverse { 2 public static void main(String[] args) { 3 int[] arr = {23 ,45 ,89 ,289 ,12 ,974 ,3 ,64 ,82}; 4 5 int[] arrReverse = reverse(arr); 6 print(arrReverse); 7 } 8 9 public static int[] reverse(int[] arr){ 10 for (int i =0;i < arr.length / 2 ;i++ ) { 11 int temp = arr[i]; 12 arr[i] = arr[arr.length-1-i]; 13 arr[arr.length-1-i] = temp; 14 } 15 return arr; 16 } 17 18 public static void print(int[] arr){ 19 for (int i = 0;i < arr.length ;i++ ) { 20 System.out.print(arr[i] + " "); 21 } 22 } 23 }
数组查表法(根据键盘录入索引,查找对应星期)
1 import java.util.Scanner; 2 class WhatDay { 3 public static void main(String[] args) { 4 Scanner sc = new Scanner(System.in); 5 System.out.println("输入要查询的日期,范围1~7"); 6 int day = sc.nextInt(); 7 8 System.out.println("今天是星期" + whatDay(day)); 9 } 10 public static char whatDay(int day){ 11 char[] arr = {'?' ,'一' ,'二' ,'三' ,'四' ,'五' ,'六' ,'日'}; 12 return arr[day]; 13 } 14 }
数组元素查找(查找指定元素第一次在数组中出现的索引)
1 import java.util.Scanner; 2 class Search { 3 public static void main(String[] args) { 4 int[] arr = {38 ,76 , 187 ,9 ,72 ,87 ,456 ,872}; 5 System.out.println("请输入一个数"); 6 Scanner sc = new Scanner(System.in); 7 int imput = sc.nextInt(); 8 9 int i = getIndex(arr ,imput); 10 System.out.println(imput + "的位置是" + i); 11 } 12 13 public static int getIndex(int[] arr ,int imput){ 14 for (int i = 0;i < arr.length ;i++ ) { 15 if (arr[i] == imput) { 16 return i; 17 } 18 } 19 return -1; 20 } 21 }
5:二维数组定义格式?
数据类型[][] 数组名 = new 数据类型[m][n];
数据类型[] 数组名[] = new 数据类型[m][n];
数据类型 数组名[][] = new 数据类型[m][n];
6:案例
A:二维数组遍历
class PrintArray{ public static void main(String[] args) { int[][] arr = {{38 ,76 , 187 ,9 ,72 ,87 ,456 ,872} ,{3 ,5 , 8} ,{10} ,{8434 ,89 ,98 ,123}}; print(arr); } public static void print(int[][] arr){ for (int i = 0;i < arr.length ;i++) { for (int j = 0 ;j < arr[i].length ;j++) { System.out.print(arr[i][j] + " "); } System.out.println(); } } }
B:公司年销售额求和
某公司按照季度和月份统计的数据如下:单位(万元)
第一季度:22,66,44
第二季度:77,33,88
第三季度:25,45,65
第四季度:11,66,99
class Sum { public static void main(String[] args) { int[][] arr = {{22,66,44} ,{77,33,88} ,{25,45,65} ,{11,66,99}}; System.out.println("销售总额为" + add(arr)); } public static int add(int[][] arr){ int sum = 0; for (int i = 0;i < arr.length ;i++) { for (int j = 0 ;j < arr[i].length ;j++) { sum += arr[i][j]; } } return sum; } }
7:参数传递问题
基本数据类型的值传递,不改变原值,因为方法调用后就会弹栈,而局部变量随之消失;
引用数据类型的值传递,改变原值,因为即使方法调用后弹栈,堆内存中的数组对象还在,仍可以通过地址(调用)进行访问.