------- android培訓、java培訓、期待與您交流! ----------
05.01 方法概述和格式說明
簡單的說:方法就是完成特定功能的代碼塊
在很多語言里面都有函數的定義,函數在Java中被稱為方法
格式:
修飾符返回值類型方法名(參數類型參數名1,參數類型參數名2...)
{
函數體;
return 返回值;
}
方法格式解釋:
修飾符:后面會詳細介紹。目前public static
返回值類型:用于限定返回值的數據類型
方法名:一個名稱,方便我們調用方法
參數類型:限定調用方法時傳入參數的數據類型
參數名:是一個變量,接收調用方法時傳入的參數
方法體:完成功能的代碼
return:結束方法以及返回方法指定類型的值
返回值:程序被return帶回的結果,返回給調用者
05.02 方法的定義求和案例
根據兩個明確來寫一個方法
1.返回值類型,明確功能結果的數據類型
2.參數列表,明確有幾個參數,以及參數的類型
例:求兩個數據之和
1 public static int add(int i,int j)2 {3 return i+j;4 }05.03 方法的調用有明確返回值的方法調用
有明確返回值的方法調用:
1. 單獨調用,沒有意義
例:add(2,3);
2. 輸出調用,有意義,但是不夠好,因為不一定要把結果輸出
例:System.out.PRintln(result);
3. 賦值調用,推薦方式
例:int result = add(2,3);
System.out.println(result);
05.04 方法的調用圖解

05.05 方法的練習1獲取兩個數中的較大值
鍵盤錄入兩個數據,返回兩個數中的較大值
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.println("請輸入第1個數:"); 8 int num1 = sc.nextInt(); 9 System.out.println("請輸入第2個數:");10 int num2 = sc.nextInt();11 int max = getMax(num1 , num2);12 System.out.println("較大的數是:"+max);13 }14 //返回值類型:int15 //參數列表:int i,int j16 public static int getMax(int i,int j)17 {18 return (i > j)? i : j;19 }20 }運行結果:
請輸入第1個數:43請輸入第2個數:56較大的數是:56
05.06 方法的練習2比較兩個數是否相等
鍵盤錄入兩個數據,比較兩個數是否相等
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.println("請輸入第1個數:"); 8 int num1 = sc.nextInt(); 9 System.out.println("請輸入第2個數:");10 int num2 = sc.nextInt();11 boolean flag = compare(num1 , num2);12 System.out.println("比較結果:"+flag);13 }14 //返回值類型:boolean15 //參數列表:int i,int j16 public static boolean compare(int i,int j)17 {18 return i == j;19 }20 }運行結果:
請輸入第1個數:34請輸入第2個數:65比較結果:false
05.07 方法的練習3獲取三個數中的最大值
鍵盤錄入三個數據,返回三個數中的最大值
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.println("請輸入第1個數:"); 8 int num1 = sc.nextInt(); 9 System.out.println("請輸入第2個數:");10 int num2 = sc.nextInt();11 System.out.println("請輸入第3個數:");12 int num3 = sc.nextInt();13 int max = getMax(num1 , num2 ,num3);14 System.out.println("最大的數是:"+max);15 }16 //返回值類型:int17 //參數列表:int a,int b,int c18 public static int getMax(int a,int b,int c)19 {20 int temp = (a > b)?a : b;21 int max = (temp > c)?temp : c;22 return max;23 }24 }運行結果:
請輸入第1個數:23請輸入第2個數:54請輸入第3個數:21最大的數是:54
05.08 方法的注意事項
1.方法不調用不執行
2.方法與方法之間是平級關系,不能嵌套定義
3.方法定義的時候參數之間用逗號隔開
4.方法調用的時候不用再傳遞數據類型
5.如果方法有明確的返回值,一定要有return返回一個值
05.09 方法的調用void類型方法的定義和調用
沒有明確返回值的函數調用:其實就是void類型方法的調用,只能單獨調用
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 print(3,4); 6 } 7 //返回值類型:沒有明確的返回值,用void 8 //參數列表:int m,int n 9 public static void print(int m,int n)10 {11 for(int i = 0;i < m;i++)12 {13 for(int j = 0;j <n;j++)14 {15 System.out.print("* ");16 }17 System.out.println();18 }19 }20 }運行結果:
* * * ** * * ** * * *
05.10 方法的練習4根據行數和列數輸出星形
鍵盤錄入行數和列數,輸出對應的星形
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.println("請輸入行數:"); 8 int row = sc.nextInt(); 9 System.out.println("請輸入列數:");10 int col = sc.nextInt();11 print(row,col);12 }13 //返回值類型:void14 //參數列表:int m,int n15 public static void print(int m,int n)16 {17 for(int i = 0;i < m;i++)18 {19 for(int j = 0;j <n;j++)20 {21 System.out.print("* ");22 }23 System.out.println();24 }25 }26 }運行結果:
請輸入行數:4請輸入列數:5* * * * ** * * * ** * * * ** * * * *
05.11 方法的練習5根據鍵盤錄入輸出對應的乘法表
鍵盤錄入一個數據n(1<=n<=9),輸出對應的nn乘法表
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 Scanner sc = new Scanner(System.in); 7 System.out.println("請輸入一個數(1~9):"); 8 int num = sc.nextInt(); 9 print(num);10 }11 //返回值類型:void12 //參數列表:int m13 public static void print(int m)14 {15 for(int i = 1;i <= m;i++)16 {17 for(int j = 1;j <= i;j++)18 {19 System.out.print(j+"*"+i+"="+(i*j)+"/t");20 }21 System.out.println();22 }23 }24 }運行結果:
請輸入一個數(1~9):31*1=11*2=2 2*2=41*3=3 2*3=6 3*3=9
05.12 方法重載概述和基本使用
方法法重載概述:在同一個類中,允許存在一個以上的同名方法,只要它們的參數個數或者參數類型不同即可。
方法重載特點:與返回值類型無關,只看方法名和參數列表,在調用時,虛擬機通過參數列表的不同來區分同名方法。
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int result1 = add(2,3); 6 System.out.println("result1 = "+result1); 7 int result2 = add(2,3,4); 8 System.out.println("result2 = "+result2); 9 }10 //求2個數的和11 public static int add(int a,int b)12 {13 return a + b;14 }15 //求3個數的和16 public static int add(int a,int b,int c)17 {18 return a + b + c;19 }20 }運行結果:
result1 = 5result2 = 9
05.13 方法重載練習比較數據是否相等
比較兩個數據是否相等
參數類型分別為兩個byte類型,兩個short類型,兩個int類型,兩個long類型,并在main方法中進行測試
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 //測試byte 6 byte b1 = 2; 7 byte b2 = 3; 8 System.out.println("byte:"+compare(b1,b2)); 9 10 //測試short11 short s1 = 5;12 short s2 = 5;13 System.out.println("short:"+compare(s1,s2));14 15 //測試int16 int i1 = 6;17 int i2 = 7;18 System.out.println("int:"+compare(i1,i2));19 20 //測試long21 long l1 = 6788;22 int l2 = 6788;23 System.out.println("long:"+compare(l1,l2));24 }25 //byte類型26 public static boolean compare(byte a,byte b) 27 {28 return a == b;29 }30 31 //short類型32 public static boolean compare(short a,short b) 33 {34 return a == b;35 }36 37 //int類型38 public static boolean compare(int a,int b) 39 {40 return a == b;41 }42 43 //long類型44 public static boolean compare(long a,long b) 45 {46 return a == b;47 }48 }05.14 數組概述和定義格式說明
數組概念:
1.數組是存儲同一種數據類型多個元素的集合,也可以看成是一個容器。
2.數組既可以存儲基本數據類型,也可以存儲引用數據類型。
數組的定義格式:
格式1:數據類型[] 數組名; 例:int[] arr;
格式2:數據類型數組名[]; 例:int arr[];
注意:這兩種定義做完了,數組中的元素是沒有值的
05.15 數組的初始化動態初始化
數組初始化概述:Java中的數組必須先初始化,然后才能使用。
初始化:就是為數組中的數組元素分配內存空間,并為每個數組元素賦值。
數組的初始化方式:
動態初始化:初始化時只指定數組長度,由系統為數組分配初始值。
靜態初始化:初始化時指定每個數組元素的初始值,由系統決定數組長度。
動態初始化:初始化時只指定數組長度,由系統為數組分配初始值。
格式:數據類型[] 數組名 = new 數據類型[數組長度];數組長度其實就是數組中元素的個數。
例:int[] arr = new int[3];
解釋:定義了一個int類型的數組,這個數組中可以存放3個int類型的值。
數組中的每個元素都是有編號的,編號從0開始到數組的長度減1,用數組名與編號的配合就可以獲取數組中指定編號的元素,編號的專業名稱叫索引,格式:數組名[索引]
05.16 Java中的內存分配以及棧和堆的區別
Java程序在運行時,需要在內存中分配空間。為了提高運算效率,就對空間進行了不同區域的劃分,因為每一片區域都有特定的處理數據方式和內存管理方式。
1.棧:存儲局部變量
2.堆:存儲new出來的東西
3.方法區:(面向對象)
4.本地方法區:(和系統相關)
5.寄存器:(給CPU使用)
局部變量:方法定義中或者方法聲明上的所有變量,使用完畢,立即消失
棧內存:存儲的都是局部變量,局部變量就是定義在方法中的變量,局部變量所屬的作用域一旦結束,該變量就自動釋放
堆內存:存儲的是數組和對象(其實數組就是對象),凡是new建立的對象都在堆中
堆內存特點:
1.每一個實體都有首地址值
2.堆內存中的每一個變量都有默認初始化值,根據數據類型的不同而不同
byte、short、int、long默認為0
float、double默認為0.0或0.0f
boolean默認為false
char默認為'/u0000'
引用類型默認為null
3.使用完畢后,會被垃圾回收器空閑的時候回收
05.17 數組的內存圖解1一個數組
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = new int[3]; 6 7 //輸出數組名稱 8 System.out.println(arr); 9 10 //輸出數組元素值11 System.out.println(arr[0]);12 System.out.println(arr[1]);13 System.out.println(arr[2]);14 15 //給數組元素賦值16 arr[0] = 100;17 arr[2] = 200;18 19 //輸出數組名稱20 System.out.println(arr);21 22 //輸出數組元素值23 System.out.println(arr[0]);24 System.out.println(arr[1]);25 System.out.println(arr[2]);26 }27 }運行結果:
[I@1175422 ( [表示數組實體 I表示Int型 1175422表示十六進制哈希值 )000[I@11754221000200
圖解:

05.18 數組的內存圖解2二個數組
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr1 = new int[2]; 6 int[] arr2 = new int[3]; 7 8 arr1[1] = 10; 9 arr2[0] = 30;10 arr2[1] = 20;11 12 System.out.println(arr1);13 System.out.println(arr1[0]);14 System.out.println(arr1[1]);15 16 System.out.println(arr2);17 System.out.println(arr2[0]);18 System.out.println(arr2[1]);19 System.out.println(arr2[2]);20 }21 }運行結果:
[I@1175422010[I@949f6930200
圖解:

05.19 數組的內存圖解3三個數組
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr1 = new int[3]; 6 arr1[0] = 10; 7 arr1[1] = 20; 8 arr1[2] = 30; 9 10 int[] arr2 = new int[3];11 arr2[0] = 50;12 arr2[1] = 60;13 arr2[2] = 70;14 15 int[] arr3 = arr1;16 arr3[0] = 100;17 arr3[1] = 200;18 19 System.out.println(arr1);20 System.out.println(arr1[0]);21 System.out.println(arr1[1]);22 System.out.println(arr1[2]);23 24 System.out.println(arr2);25 System.out.println(arr2[0]);26 System.out.println(arr2[1]);27 System.out.println(arr2[2]);28 }29 }運行結果:
[I@117542210020030[I@949f69506070
圖解:

05.20 數組的初始化靜態初始化及內存圖
靜態初始化:初始化時指定每個數組元素的初始值,由系統決定數組長度。
格式:數據類型[] 數組名 = new 數據類型[]{元素1,元素2,...};
例:int[] arr = new int[]{1,2,3};
解釋:定義了一個int類型的數組,這個數組中可以存放3個int類型的值,并且值分別是1,2,3。
簡化寫法:int[] arr = {1,2,3};
例:
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = {1,2,3}; 6 System.out.println(arr); 7 System.out.println(arr[0]); 8 System.out.println(arr[1]); 9 System.out.println(arr[2]);10 }11 }運行結果:
[I@1175422123
圖解:

注意:不能同時進行動態初始化和靜態初始化
05.21 數組操作的兩個常見小問題越界和空指針

05.22 數組的操作1遍歷
數組遍歷(依次輸出數組中的每一個元素)
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = {12,32,11,5,23}; 6 System.out.print("["); 7 //arr.length返回數組的長度 8 for(int i = 0;i < arr.length;i++) 9 {10 if(i == arr.length-1)11 System.out.println(arr[i]+"]");12 else13 System.out.print(arr[i]+",");14 }15 }16 }運行結果:
[12,32,11,5,23]
05.23 數組的操作2獲取最值
數組獲取最值(獲取數組中的最大值最小值)
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = {12,32,11,5,23,56,15,27}; 6 //假設第1個數是最大的 7 int max = arr[0]; 8 //從第2個數開始遍歷,依次與max比較 9 for(int i = 1;i < arr.length;i++)10 {11 if(arr[i] > max)12 max = arr[i];13 }14 System.out.println("最大的數是:"+max);15 }16 }運行結果:
最大的數是:56
05.24 數組的操作3逆序
數組元素逆序 (就是把元素對調)
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = {12,3,1,35,23}; 6 System.out.print("逆序前:"); 7 print(arr); 8 reverse2(arr); 9 System.out.print("逆序后:");10 print(arr);11 }12 //方法113 public static void reverse1(int[] arr)14 {15 for(int i = 0;i < arr.length / 2;i++)16 {17 int temp = arr[i];18 arr[i] = arr[arr.length-1-i];19 arr[arr.length-1-i] = temp;20 }21 }22 //方法223 public static void reverse2(int[] arr)24 {25 for(int start = 0,end = arr.length - 1;start <= end;start++,end--)26 {27 int temp = arr[start];28 arr[start] = arr[end];29 arr[end] = temp;30 }31 }32 //遍歷數組的方法33 public static void print(int[] arr)34 {35 System.out.print("[");36 for(int i = 0;i < arr.length;i++)37 {38 if(i == arr.length-1)39 System.out.println(arr[i]+"]");40 else41 System.out.print(arr[i]+",");42 }43 }44 }運行結果:
逆序前:[12,3,1,35,23]逆序后:[23,35,1,3,12]
05.25 數組的操作4查表法
數組查表法(根據鍵盤錄入索引,查找對應星期)
1 import java.util.Scanner; 2 class Demo 3 { 4 public static void main(String[] args) 5 { 6 //定義一個字符串數組 7 String[] strArray = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"}; 8 Scanner sc = new Scanner(System.in); 9 System.out.println("請輸入一個數(1~7):");10 int index = sc.nextInt();11 if(index > 7 || index < 1)12 System.out.println("數據輸入錯誤");13 else14 System.out.println(index+"對應"+strArray[index-1]);15 }16 }運行結果:
請輸入一個數(1~7):11對應星期一
05.26 數組的操作5基本查找
數組元素查找(查找指定元素第一次在數組中出現的索引)
1 class Demo 2 { 3 public static void main(String[] args) 4 { 5 int[] arr = {23,12,26,73,14,27}; 6 int index = getIndex(arr,14); 7 if(index == -1) 8 System.out.println("沒有找到該數"); 9 else10 System.out.println("找到該數,在第"+index+"位");11 }12 //返回值類型:int13 //參數列表:int[] arr,int key14 public static int getIndex(int[] arr,int key)15 {16 for(int i = 0;i < arr.length;i++)17 {18 if(arr[i] == key)19 return i;20 }21 //沒有找到返回-122 return -1;23 }24 }運行結果:
找到該數,在第4位
新聞熱點
疑難解答