<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                數組定義:能夠保存一組數據 特點:1.數據必須同類型;2.固定長度; *** ### 數組定義方式: ~~~ public class ArrayDemo1 { public static void main(String[] args) { // 數組:一個容器,能夠放一組數據 // 特點(缺點):1.必須是同類型;2.固定長度 // 聲明方法1:數組類型 數組名[] = new 數組類型[長度];數組類型[] 數組名 = new 數組類型[長度]; int arr1[] = new int[5];// 默認保存了5個0 // String str[] = new String[5];// 默認保存了5個null // boolean b[] = new boolean[5];// 默認保存了5個false // 通過下標值去訪問數組元素:長度為n的數組,下標值范圍0-->n-1 arr1[0] = 120; arr1[1] = 121; arr1[2] = 122; arr1[3] = 123; arr1[4] = 124; // arr1[5] = 125;// 數組越界 // System.out.println(arr1[0]); // 聲明方法2:數組類型 數組名[] = new 數組類型[]{元素1,元素2,....}; int arr2[] = new int[] {120,121,122,123,124}; System.out.println(arr2[3]); // 聲明方法3:數組類型 數組名[] = {元素1,元素2,....}; int arr3[] = {120,121,122,123,124,125}; // 數組的length屬性 System.out.println(arr3.length); // 字符串的length()方法 System.out.println("hello".length()); } } ~~~ ### 數組常見操作--遍歷 ~~~ public class ArrayDemo2 { public static void main(String[] args) { // 數組的遍歷(取到數組中每一個元素) // length遍歷次數-->數組名[下標] int arr[] = {5,7,23,98,111,13,77}; // 遍歷方法1: // for(int i = 0;i < arr.length;i++) { // int num = arr[i]; // System.out.println(num); // } // 遍歷方法2:增強for循環 for(int num:arr) { System.out.println(num); } String str[] = {"ab","cd","ef","g"}; for(String s:str) { System.out.println(s); } } } ~~~ ### 一維數組內存模型 ![](https://box.kancloud.cn/33f0c8426c3f658e45ab283d25a2c81f_688x419.png) ### 二維數組內存模型 ![](https://box.kancloud.cn/100f7b53ee174fb0380fecdc282dfaad_691x419.png) ### 二維數組的定義及遍歷 ~~~ public class ManyArrayDemo1 { public static void main(String[] args) { // 二維數組 // int arr1[][] = {{1,2,3},{4},{5,6,7,8}}; // System.out.println(arr1.length);// 5 // 第二種聲明方式 // int[][] arr2 = new int[3][]; // arr2[0] = new int[]{1,2,3}; // arr2[1] = new int[]{4,5,6}; // arr2[2] = new int[]{7,8,9,10,11,12}; // 二維數組的訪問 // System.out.println(arr2[2][2]); // 二維數組遍歷 // for(int i = 0;i < arr1.length;i++) { // for(int j = 0;j < arr1[i].length;j++) { // System.out.print(arr1[i][j]+" "); // } // System.out.println(); // } } } ~~~ ### 排序(冒泡,選擇) ~~~ public class ArrayDemo4 { public static void main(String[] args) { // 1.冒泡排序 // int[] arr={6,3,8,2,9,1}; // for(int i = 0;i < arr.length - 1;i++) { // for(int j = 0;j < arr.length - i - 1;j++) { // if(arr[j] < arr[j+1]) { // int temp; // temp = arr[j]; // arr[j] = arr[j+1]; // arr[j+1] = temp; // } // } // } // for(int num:arr) { // System.out.println(num); // } // 2.選擇排序 int[] arr={6,3,8,2,9,1}; // 1-1 3,6,8,2,9,1 1-2 3,6,8,2,9,1 1-3 2,6,8,3,9,1 1-4 2,6,8,3,9,1 1-5 1,6,8,3,9,2 // 2-1 1,6,8,3,9,2 2-2 1,3,8,6,9,2 2-3 1,3,8,6,9,2 2-4 1,2,8,6,9,3 for(int i = 0;i < arr.length - 1;i++) { for(int j = i + 1;j < arr.length;j++) { if(arr[i] > arr[j]) { int temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (int num : arr) { System.out.println(num); } } } ~~~ ### 快速排序及數組拷貝 ~~~ public class ArrayDemo5 { public static void main(String[] args) { int arr[] = {7,5,3,2,1,9,10}; Arrays.sort(arr);// 快速排序--升序 for(int num:arr) { System.out.println(num); } } } ~~~ ~~~ public class ArrayCopyDemo1 { public static void main(String[] args) { // 數組的復制 int source[] = {1,2,3,4,5,6}; int dest[] = {7,8,9,10,11,12,13,14,15}; /* * source:源數組 * srcPos:源數組中的起始位置 * dest:目標數組 * destPos:目標數組中的起始位置 * length:要復制的數組元素的個數 */ System.arraycopy(source, 1, dest, 3, 3); for(int num:dest) { System.out.print(num+" "); } } } ~~~ *** ### 課外習題 1.模擬35選7 ~~~ public class Practice5 { public static void main(String[] args) { // 模擬35選7(沒有循環嵌套) boolean bool[] = new boolean[35];// 35個false for(int i = 0;i < 7;i++) { // 生成一個隨機數 int random = (int)(Math.random()*35+1);// 5 if(bool[random-1]) { i--; continue; } bool[random-1] = true; } // for(boolean b:bool) { // System.out.print(b+" "); // } for(int i = 0;i < bool.length;i++) { if(bool[i]) { System.out.println(i+1); } } } } ~~~ 2.編寫一個簡單程序,要求數組長度為5,分別 賦值10,20,30,40,50,在控制臺輸出該數組的值。 ~~~ public class HomeWork1 { public static void main(String[] args) { // 1、編寫一個簡單程序,要求數組長度為5,分別賦值10,20,30,40,50,在控制臺輸出該數組的值。 int arr[] = {10,20,30,40,50}; for(int num:arr) { System.out.println(num); } } } ~~~ 3.給定一個有9個整數(1,6,2,3,9,4,5,7,8)的數組 ,先排序,然后輸出排序后的數組的值。 ~~~ public class HomeWork3 { public static void main(String[] args) { // 3、給定一個有9個整數(1,6,2,3,9,4,5,7,8)的數組,先排序,然后輸出排序后的數組的值。 int arr[] = {1,6,2,3,9,4,5,7,8}; for(int i = 0;i < arr.length - 1;i++) { for(int j = i + 1;j < arr.length;j++) { if(arr[i] < arr[j]) { int temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for(int num:arr) { System.out.println(num); } } } ~~~ 4.在控制臺上輸入一個正整數,代表數組長度,然后向數組中輸入每一個元素,然后在控制臺上遍歷出來。 ~~~ public class Practice8 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("請輸入要輸入幾個數字:"); int num = scan.nextInt(); int arr[] = new int[num]; int index = 0; for(int i = 0;i < num;i++) { System.out.println("請輸入第"+(i+1)+"個數字"); arr[index] = scan.nextInt(); index++; } for(int n:arr) { System.out.print(n+" "); } } } ~~~ 5.利用數組完成此題:控制臺輸入一年當中的第幾月多少號,輸出這天是這一年的多少天。 ~~~ public class Practice9 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("year:"); int year = scan.nextInt(); System.out.println("month:"); int month = scan.nextInt(); System.out.println("date:"); int date = scan.nextInt(); int feb = 28; // 閏年29天 if(year%4==0 && year%100!=0 || year%400==0) { feb = 29; } int days[] = {31,feb,31,30,31,30,31,31,30,31,30,31}; int sum = 0; for(int i = 0;i < month - 1;i++) { sum = sum + days[i]; } System.out.println("第"+(sum+date)+"天"); } } ~~~ 6.隨機生成100個1到10的隨機數,計算這些數字出現的概率。 ~~~ public class Test { public static void main(String[] args) { int arr[] = new int[10]; for(int i = 0;i < 100;i++){ int random = (int)(Math.random()*6)+1; arr[random-1]++; } for(int i = 0;i < arr.length;i++){ System.out.println(i+1+"出現的概率是"+arr[i]+"%"); } } } ~~~ *** #### 總結: 1.一維,二維數組的定義及遍歷; 2.對數組排序(循環嵌套);
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看