Java流程控制語句和數組整理
7.1選擇結構switch
switch (表達式){
case 目標值1:
執行語句1
break;
case 目標值2:
執行語句2
break;
......
case 目標值n:
執行語句n
break;
default:
執行語句n+1
break;
}
在switch語句中的表達式只能是byte、short、char、int、Sting、enum枚舉類型的值
7.2選擇結構switch練習
在使用switch語句的過程中,如果多個case條件後面的執行語句是一樣的,則該執行語句只需書寫一次即可,這是一種簡寫的方式。例如,要判斷一周中的某一天是否為工作日,同樣使用數字1~7來表示星期一到星期天,當輸入的數字為1、2、3、4、5時就視為工作日,否則就視為休息日。接下來通過一個案例來實現上面描述的情況,如下所示。SwitchDemo02.java
public class SwitchDemo02 {
public static void main(String[] args) {
int week = 2;
switch (week) {
case 1:
case 2:
case 3:
case 4:
case 5:
// 當 week 滿足值 1、2、3、4、5 中任意一個時,處理方式相同
System.out.println("今天是工作日");
break
case 6:
case 7:
// 當 week 滿足值 6、7 中任意一個時,處理方式相同
System.out.println("今天是休息日");
break;
}
}
}
運行結果:
數組
8.1數組的定義
定義格式:
數據類型[] 數組名 = new 數據類型[元素個數或數組長度];
1 public class ArrayDemo01 {
2 public static void main(String[] args) {
3 int[] arr; // 聲明變量
4 arr = new int[3]; // 創建數組對象
5 System.out.println("arr[0]=" + arr[0]); // 訪問數組中的第一個元素
6 System.out.println("arr[1]=" + arr[1]); // 訪問數組中的第二個元素
7 System.out.println("arr[2]=" + arr[2]); // 訪問數組中的第三個元素
8 System.out.println("數組的長度是:" + arr.length); // 打印數組長度
9 }
10 }
運行結果:
1、類型[] 數組名 = new 類型[]{元素,元素,……};
2、類型[] 數組名 = {元素,元素,元素,……};
1 public class ArrayDemo03 {
2 public static void main(String[] args) {
3 int[] arr ={ 1, 2, 3, 4 }; // 靜態初始化
4 // 下面的代碼是依次訪問數組中的元素
5 System.out.println("arr[0] = " + arr[0]);
6 System.out.println("arr[1] = " + arr[1]);
7 System.out.println("arr[2] = " + arr[2]);
8 System.out.println("arr[3] = " + arr[3]);
9 }
10 }
運行結果:
8.2數組遍歷
public class ArrayDemo04 {
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5 }; // 定義數組
// 使用for循環遍歷數組的元素
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]); // 通過索引訪問元素
}
}
}
運行結果:
8.3數組的常見問題
8.3.1數組最值
public class ArrayDemo05 {
public static void main(String[] args) {
int[] arr = { 4, 1, 6, 3, 9, 8 }; // 定義一個數組
int max = arr[0]; // 定義變量max用於記住最大數,首先假設第一個元素為最大值
// 下面通過一個for循環遍歷數組中的元素
for (int x = 1; x < arr.length; x++) {
if (arr[x] > max) { // 比較 arr[x]的值是否大於max
max = arr[x]; // 條件成立,將arr[x]的值賦給max
}
}
System.out.println("max=" + max); // 打印最大值
}
}
運行結果:
8.3.2數組異常
8.3.2.1數組越界異常
1 public class ArrayDemo06 {
2 public static void main(String[] args) {
3 int[] arr = new int[4]; // 定義一個長度為4的數組
4 System.out.println("arr[0]=" + arr[4]); // 通過角標4訪問數組元素
5 }
6 }
運行結果:
上圖運行結果中所提示的錯誤信息是數組越界異常ArrayIndexOutOfBoundsException,出現這個異常的原因是數組的長度為4,其索引範圍為0~3,而上述代碼中的第4行代碼使用索引4來訪問元素時超出了數組的索引範圍。
所謂異常指程序中出現的錯誤,它會報告出錯的異常類型、出錯的行號以及出錯的原因,關於異常在後面的章節會有詳細地講解。
8.2.2.2空指針異常
在使用變量引用一個數組時,變量必須指向一個有效的數組對象,如果該變量的值為null,則意味著沒有指向任何數組,此時通過該變量訪問數組的元素會出現空指針異常
1 public class ArrayDemo07 {
2 public static void main(String[] args) {
3 int[] arr = new int[3]; // 定義一個長度為3的數組
4 arr[0] = 5; // 為數組的第一個元素賦值
5 System.out.println("arr[0]=" + arr[0]); // 訪問數組的元素
6 arr = null; // 將變量arr置為null
7 System.out.println("arr[0]=" + arr[0]); // 訪問數組的元素
8 }
9 }
運行結果:
8.3二維數組
8.3.1二維數組常用定義格式
int[][] arr = {{1,2},{3,4,5,6},{7,8,9}};
上面的二維數組中定義了三個元素,這三個元素都是數組,分別為{1,2}、{3,4,5,6}、{7,8,9},接下來通過一個圖來表示這種情況,如圖2-54所示。
問二維數組中第一個元素數組的第二個元素
arr[0][1];
8.3.2二維數組元素的訪問
class ArrayDemo08 {
public static void main(String[] args){
//定義二維數組的方式
int[][] arr = new int[3][4];
System.out.println( arr );
System.out.println("二維數組的長度: " + arr.length);
//獲取二維數組的3個元素
System.out.println( arr[0] );
System.out.println( arr[1] );
System.out.println( arr[2] );
System.out.println("打印第一個一維數組的元素值");
System.out.println( arr[0][0] );
System.out.println( arr[0][1] );//訪問的為二維數組中第1個一維數組的第2個元素
System.out.println( arr[0][2] );
System.out.println( arr[0][3] );
System.out.println("打印第二個一維數組的元素值");
System.out.println( arr[1][0] );
System.out.println( arr[1][1] );
System.out.println( arr[1][2] );
System.out.println( arr[1][3] );
System.out.println("打印第三個一維數組的元素值");
System.out.println( arr[2][0] );
System.out.println( arr[2][1] );
System.out.println( arr[2][2] );
System.out.println( arr[2][3] );
}
}
運行結果:
8.3.3二維數組元素遍歷與數組元素累加和
class ArrayDemo09 {
public static void main(String[] args){
//一維數組的求累加和並遍歷
int[] arr = {10,20,30,40,50};
int sum = 0;
for (int i=0; i<arr.length; i++) {
//System.out.println(arr[i]);
sum += arr[i];
}
System.out.println("sum= " + sum);
System.out.println("---------------------");
//二維數組的求累加和並遍歷
int[][] arr2 = { {1,2},{3,4,5},{6,7,8,9,10} };
int sum2 = 0;
for (int i=0; i<arr2.length; i++) {
for (int j=0; j<arr2[i].length; j++) {
//System.out.println(arr2[i][j])
sum2 += arr2[i][j];
}
}
System.out.println("sum2= "+ sum2);
}
}
運行結果:
Java流程控制語句和數組整理