一、案例一
案例說明 | 案例:定義一個int型的一維數組,包含10個元素,分別賦一些隨機整數,然后求出所有元素的最大值,最小值,總和,平均值,并輸出出來 |
代碼實現 | public class ArrayExer01 { ? ? ? ? System.out.println(); ? ? ? ? //3.1 求最大值 ? ? ? ? //3.2 求最小值
? ? ? ? //3.4 求平均值
|
二、案例二
案例說明 | 案例:評委打分 分析以下需求,并用代碼實現: (1)在編程競賽中,有10位評委為參賽的選手打分,分數分別為:5,4,6,8,9,0,1,2,7,3 (2)求選手的最后得分(去掉一個最高分和一個最低分后其余8位評委打分的平均值) |
代碼實現 | public class ArrayExer02 { ? ? ? ? int[] scores = {5,4,6,8,9,0,1,2,7,3}; ? ? ? ? for (int i = 0; i < scores.length; i++) { ? ? ? ? int avg = (sum - max - min) / (scores.length - 2);
|
三、案例三
案例說明 | 找最值及其所有最值的下標 |
代碼實現一 | public class Test13AllMaxIndex { ? ? ? ? //遍歷數組,看哪些元素和最大值是一樣的 |
代碼實現二 | public class Test13AllMaxIndex2 { ? ? ? ? System.out.println("最大值是" + max); |
四、案例四
案例說明 |
? ?提示: |
代碼實現 | public class YangHuiTest { ? ? ? ? //1. 創建二維數組 ? ? ? ? //2.使用循環結構,初始化外層數組元素 ? ? ? ? }
|
五、案例五
案例說明 | 案例:復制、賦值 使用簡單數組 思考:array1和array2是什么關系? 拓展:修改題目,實現array2對array1數組的復制 |
代碼實現(賦值) | public class ArrayExer04 { ? ? ? ? for (int i = 0; i < array2.length; i++) { ? ? ? ? System.out.println();//換行 |
代碼實現(復制) | public class ArrayExer04_1 { ? ? ? ? System.out.println(); ? ? ? ? for (int i = 0; i < array2.length; i++) { ? ? ? ? System.out.println();//換行 |
六、案例六
案例說明 | 案例: 定義數組:int[] arr = new int[]{34,54,3,2,65,7,34,5,76,34,67}; 如何實現數組元素的反轉存儲?你有幾種方法。 |
代碼實現1 | public class ArrayExer05 { ? ? ? ? //遍歷 ? ? ? ? //反轉操作 |
代碼實現2 | public class ArrayExer05 { ? ? ? ? //遍歷 ? ? ? ? //反轉操作 |
代碼實現3 | public class ArrayExer05 { ? ? ? ? //遍歷 ? ? ? ? //反轉操作 ? ? ? ? //方式3:不推薦 ? ? ? ? arr = newArr; ? ? ? ? //遍歷 |
七、案例七
案例說明 | 一個數組,讓數組的每個元素去除第一個元素,得到的商作為被除數所在位置的新值。 |
代碼實現 | public class Test3 { ?? ?public static void main(String[] args) { ?? ??? ?int[] arr = new int[]{12,43,65,3,-8,64,2}; ?? ??? ? //?? ??? ?for(int i = 0;i < arr.length;i++){ //?? ??? ??? ?arr[i] = arr[i] / arr[0]; //?? ??? ?} ?? ??? ?for(int i = arr.length -1;i >= 0;i--){ ?? ??? ??? ?arr[i] = arr[i] / arr[0]; ?? ??? ?} ?? ??? ?//遍歷arr ?? ??? ?for(int i = 0;i < arr.length;i++){ ?? ??? ??? ?System.out.print(arr[i] + " "); ?? ??? ?} ?? ?} } |
七、案例七
案例說明 | 創建一個長度為6的int型數組,要求數組元素的值都在1-30之間,且是隨機賦值。同時,要求元素的值各不相同。 |
代碼實現 | public class Test4 { ?? ??? ??? ?boolean flag = false; ?? ??? ?for (int i = 0; i < arr.length; i++) { ?? ??? ?for (int i = 0; i < arr.length; i++) { |
八、案例八:回形數
案例說明 | 從鍵盤輸入一個整數(1~20) ,則以該數字為矩陣的大小,把1,2,3…n*n 的數字按照順時針螺旋的形式填入其中。 例如: 輸入數字2,則程序輸出: 1 2 4 3 輸入數字3,則程序輸出: 1 2 3 8 9 4 7 6 5 輸入數字4, 則程序輸出: 1? ?2? ? 3? ?4 12 13 14? 5 11?16 15? 6 10? 9? ?8? ?7 |
代碼實現一 | //方式1 public class RectangleTest { ?? ?public static void main(String[] args) { ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?System.out.println("輸入一個數字"); ?? ??? ?int len = scanner.nextInt(); ?? ??? ?int[][] arr = new int[len][len]; ?? ??? ? ?? ??? ?int s = len * len; ?? ??? ?/* ?? ??? ? * k = 1:向右 ?? ??? ? * k = 2:向下 ?? ??? ? * k = 3:向左 ?? ??? ? * k = 4:向上 ?? ??? ? */ ?? ??? ?int k = 1; ?? ??? ?int i = 0,j = 0; ?? ??? ?for(int m = 1;m <= s;m++){ ?? ??? ??? ?if(k == 1){ ?? ??? ??? ??? ?if(j < len && arr[i][j] == 0){ ?? ??? ??? ??? ??? ?arr[i][j++] = m; ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?k = 2; ?? ??? ??? ??? ??? ?i++; ? ?? ??? ??? ??? ??? ?j--; ?? ??? ??? ??? ??? ?m--; ?? ??? ??? ??? ?} ?? ??? ??? ?}else if(k == 2){ ?? ??? ??? ??? ?if(i < len && arr[i][j] == 0){ ?? ??? ??? ??? ??? ?arr[i++][j] = m; ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?k = 3; ?? ??? ??? ??? ??? ?i--; ?? ??? ??? ??? ??? ?j--; ?? ??? ??? ??? ??? ?m--; ?? ??? ??? ??? ?} ?? ??? ??? ?}else if(k == 3){ ?? ??? ??? ??? ?if(j >= 0 && arr[i][j] == 0){ ?? ??? ??? ??? ??? ?arr[i][j--] = m; ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?k = 4; ?? ??? ??? ??? ??? ?i--; ?? ??? ??? ??? ??? ?j++; ?? ??? ??? ??? ??? ?m--; ?? ??? ??? ??? ?} ?? ??? ??? ?}else if(k == 4){ ?? ??? ??? ??? ?if(i >= 0 && arr[i][j] == 0){ ?? ??? ??? ??? ??? ?arr[i--][j] = m; ?? ??? ??? ??? ?}else{ ?? ??? ??? ??? ??? ?k = 1; ?? ??? ??? ??? ??? ?i++; ?? ??? ??? ??? ??? ?j++; ?? ??? ??? ??? ??? ?m--; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ? ?? ??? ?//遍歷 ?? ??? ?for(int m = 0;m < arr.length;m++){ ?? ??? ??? ?for(int n = 0;n < arr[m].length;n++){ ?? ??? ??? ??? ?System.out.print(arr[m][n] + "\t"); ?? ??? ??? ?} ?? ??? ??? ?System.out.println(); ?? ??? ?} ?? ?} } |
代碼實現二 | //方式2 ?? ?public static void main(String[] args) { |