# JAVA Arrays陣列 For&For-each ###### tags: `Java` --- ## 陣列表示方法 <span class="code1">String[] name;</span> <span class="code1">byte []b;</span> <span class="code1">Char hex[];</span> ## 提取陣列資料 #### For * 適合提取部分資料 >練習使用For迴圈提取陣列元素 ```java= package Ex08; public class Ex08_Arrays_For { public static void main(String[] args) { int[] n = new int[]{56, 45, 68, 32}; //設定陣列n的值是n[0]=56, n[1]=45, n[2]=68, n[3]=32 int sum = 0; //設定一個整數sum來計算總額 //提取陣列元素JAVA使用For與For-each的迴圈 for (int i =0; i<4; i++) //for迴圈設定當i小於4,i會重複執行加1到條件不符中斷迴圈 { System.out.println("n[" +i+ "]=" +n[i]); //印出陣列n的值 sum+= n[i]; //總和等於sum+n[0~3]的值 } System.out.println("n陣列元素總合為" +sum); //印出總和 } } ``` >結果: n[0]=56 n[1]=45 n[2]=68 n[3]=32 n陣列元素總合為201 --- #### For-each * 適合提取全部資料 ```java= package Ex08; public class Ex08_Arrays_For_each { public static void main(String[] args) { int[] n = new int[]{56, 45, 68, 32}; //設定陣列n的值是n[0]=56, n[1]=45, n[2]=68, n[3]=32 int sum = 0; //設定一個整數sum來計算總額 //提取陣列元素JAVA使用For與For-each的迴圈 for (int i :n) //for-each迴圈(資料型別+變數:陣列名稱) { System.out.println(i); //印出陣列n的值 sum+= i; //總和等於sum+n[0~3]的值 } System.out.println("n陣列元素總合為" +sum); //印出總和 } } ``` >結果: 56 45 68 32 n陣列元素總合為201 --- ## 多維陣列表示方法 寫法一 <span class="code1">int[][] n;</span> <span class="code1">n = new int[3][4];</span> //建立3x4的陣列 寫法二(合併成一行) <span class="code1">int[][] n = new int [3][4];</span> ![](https://i.imgur.com/dXcOswx.png) >For的寫法: ```java= package Ex08; public class Ex08_Arrays_many { public static void main (String[] args){ int[][] n = {{0,1,2,3},{4,5,6,7},{8,9,10,11}}; int sum = 0; for (int i=0; i<n.length; i++) { for (int j=0; j<n[0].length; j++) { System.out.println("n[" +i+ "][" +j+ "]=" +n[i][j]+ "," ); sum += n[i][j]; } System.out.println(); } System.out.println("n陣列元素總和為" +sum); } } ``` >For-each的寫法: ```java= package Ex08; public class Ex08_Arrays_many { public static void main (String[] args){ int[][] n = {{0,1,2,3},{4,5,6,7},{8,9,10,11}}; int sum = 0; for (int[] r :n) { //設定陣列r為提取陣列n的變數值 for (int i : r) { //設定變數i提取陣列r的值 sum += i; } System.out.println(); } System.out.println("n陣列元素總和為" +sum); } } ``` --- ## 提取非對稱型陣列 >使用For迴圈提取不對稱的陣列~ >![](https://i.imgur.com/TnPtmK3.png) ```java= package Ex08; public class Ex08_Arrays_asymmetric { public static void main(String[] args) { int[][] n = new int[3][]; n[0] = new int[]{1}; n[1] = new int[]{2,3}; n[2] = new int[]{4,5,6}; for(int i = 0; i<n.length; i++){ for(int j=0; j<n[i].length; j++){ System.out.println("n["+i+"]["+j+"]=" +n[i][j]); }; }; } } ``` >結果: n[0][0]=1 n[1][0]=2 n[1][1]=3 n[2][0]=4 n[2][1]=5 n[2][2]=6 ## 氣泡排序法 ```java= package Ex08; public class Ex08_Arrays_BubbleSort { public static void main(String[] args) { int[] x = {32,24,11,48,15}; System.out.println("【排序前】"); for(int a=0; a<x.length;a++) { System.out.print(" " + x[a]); } System.out.println(); //氣泡排序法的公式,也可以寫成 for(int i = x.length-1; i>0; i--) { // for (int j = 0; j < i; j++) { ... }} for(int i = x.length-2; i>=0; i--) { for (int j = 0; j <= i; j++) { if (x[j] > x[j+1]) { int t = x[j]; x[j] = x[j+1]; x[j+1] = t; } } System.out.println("第" + (4 - i) + "次循環:"); for (int a = 0; a < x.length; a++) System.out.print(" " + x[a]); System.out.println(); } System.out.println("【排序後】"); for (int a = 0; a < x.length; a++) System.out.print(" " + x[a]); } } ``` >結果: 【排序前】 32 24 11 48 15 第1次循環: 24 11 32 15 48 第2次循環: 11 24 15 32 48 第3次循環: 11 15 24 32 48 第4次循環: 11 15 24 32 48 【排序後】 11 15 24 32 48 Process finished with exit code 0 ~~小小OS:希望不要再用i跟j做講義了,截圖模糊i跟j傻傻分不清唷!!~~ --- ## 循序搜尋法 * Step by step一個接一個地找~ * 未排序與資料量小適合使用 ```java= package Ex08; import java.util.Scanner; public class Ex08_Arrays_Step_by_step { public static void main(String[] args) { int[] a = new int[] {5,3,1,2,10,9,4,8,7,6}; for(int i=0; i<a.length;i++){ System.out.println("第" +(i+1)+ "值數=" +a[i]); if (i==4 || i==9){ System.out.println(); } } Scanner scn = new Scanner(System.in); System.out.println("請輸入要搜尋的數字:"); int search = scn.nextInt(); int num = -1; for(int j=0; j<a.length;j++){ if(a[j] == search){ num=j; break; } } System.out.println("==================="); if(num==-1){ System.out.println("查無此數字-->"+search); }else{ System.out.println(search+"是第" +(num+1)+ "個數字"); } scn.close(); } } ``` >結果: 第1值數=5 第2值數=3 第3值數=1 第4值數=2 第5值數=10 >第6值數=9 第7值數=4 第8值數=8 第9值數=7 第10值數=6 >請輸入要搜尋的數字: --- ## 二分搜尋法 * 必須先將陣列進行排序。 * 二分搜尋法執行效率比循序搜尋法好。 * N 筆資料進行二分搜尋法,平均做 Log2N+1 次比較。 ```java= package Ex08; import java.util.Scanner; public class Ex08_Arrays_Binary_search { public static void main(String[] args) { int[] aNum = {23,100,58,11,67,12,44,101,75}; System.out.println("【排序前】"); for (int i=0; i<aNum.length; i++) { System.out.println(" " + aNum[i]); } for (int i=aNum.length-1; i>0; i--){ for (int j=0; j<i; j++){ if(aNum[j] > aNum[j+1]){ int t=aNum[j]; aNum[j]=aNum[j+1]; aNum[j+1]=t; } } } System.out.println("【排序後】"); for(int i=0; i<aNum.length; i++) { System.out.print(" " + aNum[i]); System.out.println(); } Scanner scn = new Scanner(System.in); System.out.println("請輸入要搜尋的數字"); int sNum = scn.nextInt(); int num = -1, low=0, high=aNum.length-1, midNum=0; do { midNum=(low+high)/2; if (aNum[midNum]==sNum){ num=midNum; break; } if (aNum[midNum]>sNum){ high=midNum-1; }else { low=midNum+1; } }while (low<=midNum+1); if(num==-1){ System.out.println("沒有這個數字"); }else{ System.out.println("排序後找到" +sNum+ "是第" +(num+1)+ "個數字"); } scn.close(); } } ``` --- ## Arrays 類別的應用 #### Arrays.sort() 遞增排序:<span class="code1">Arrays.sort(*陣列名稱*);</span> 遞減排序:<span class="code1">Arrays.sort(*陣列名稱*, Collections.reverseOrder());</span> ```java= package Ex08; import java.util.Arrays; public class Ex08_sort { public static void main(String[] args) { int[] num = new int[]{1,9,6,2,8,4}; Arrays.sort(num); for(int n :num){ System.out.print(n+", "); } } } ``` >結果: 1, 2, 4, 6, 8, 9, #### Arrays.binarySearch(); <span class="code1">Arrays.binarySearch(*陣列名稱*, *搜尋值*);</span> ```java= package Ex08; import java.util.Arrays; import java.util.Scanner; public class Ex08_Arrays_binarySearch { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int[] arr = {83, 25, 31, 5, 52, 17, 42, 63, 11, 9}; System.out.println("【排序前】"); for (int i =0; i<arr.length; i++){ System.out.print(arr[i]+ ", "); } System.out.println(); Arrays.sort(arr); System.out.println("【排序後】"); for (int i = 0; i<arr.length;i++){ System.out.print(arr[i]+ ", "); } System.out.println(); System.out.print("請輸入搜尋值"); int sNum = scn.nextInt(); int find = -1; if((find = Arrays.binarySearch(arr, sNum))>-1){ System.out.println("找到" +sNum+ "位於索引" +find+ "處"); }else{ System.out.println("找不到" +sNum+ "處" +find); } scn.close(); } } ``` >結果: 【排序前】 83, 25, 31, 5, 52, 17, 42, 63, 11, 9, 【排序後】 5, 9, 11, 17, 25, 31, 42, 52, 63, 83, 請輸入搜尋值 --- <span class="code1"></span> <style> h2 { color: #2383B8; } h3 { color: #1AA340; } h4 { color: white; background-color: #2383B8; padding:8px; } .code1 { padding: 2px 4px; font-size: 90%; color: #c7254e; background-color: #f9f2f4; border-radius: 4px; font-family:'Fira Code'; } .code { padding: 2px 4px; font-size: 90%; font-family:'Fira Code'; } </style>