java 数组习题
2.寫一個函數,返回一個整數數組的平均值
package com.fxm.day05.test; public class Day05test{public static void main(String[] args){int[] a1 = {2,9,5,7,4};int n = average(a1);System.out.println(n);}public static int average(int[] a){int sum = 0;for(int i = 0; i < a.length; i++){sum += a[i];}int age = sum/a.length;return age;} }3.寫一個函數接受一個整數數組a,以及一個整數n,如果n在數組中存在,則返回n出現的首 次的下標;如果不存在,則返回-1。
package com.fxm.day05.test; public class Day05Test3{public static void main(String[] args){int[] a = {2,4,5,6,8};int n = 7;int n1 = test3(a,n);System.out.println(n1);}public static int test3(int[] a,int b){for(int i = 0; i < a.length; i++){if(a[i] == b){System.out.println("找到了");return i;}}return -1;} }4.寫一個函數接受一個數組,打印輸出數組中的大值和小值。
package com.fxm.day05.test; public class Day05Test4{public static void main(String[] args){int[] a = {3,5,1,7,5,8};test4(a);}public static void test4(int[] a){int max = a[0];int min = a[0];for(int i = 0; i < a.length; i++){if(max < a[i]){max = a[i];}if(min > a[i]){min = a[i];}}System.out.println(max);System.out.println(min);} }5.寫一個函數接受一個數組,把這個數組中所有元素順序進行顛倒。
package com.fxm.day05.test; public class Day05Test5{public static void main(String[] args){int[] a = {2,4,6,8,9};test5(a);}public static void test5(int[] a){for(int i = a.length - 1; i >= 0; i--){System.out.println(a[i]);}} }7.將上面所有習題使用可變長參數替換數組實現
package com.fxm.day05.test; public class Day05Test6{public static void main(String[] args){int[] a = {2,9,5,7,4};int n1 = average(a);System.out.println(n1);int n = 7;int n2 = test3(n,a);System.out.println(n2);test4(a);test5(a);}public static int average(int... a){int sum = 0;for(int i = 0; i < a.length; i++){sum += a[i];}int age = sum/a.length;return age;}public static int test3(int b,int... a){for(int i = 0; i < a.length; i++){if(a[i] == b){System.out.println("找到了");return i;}}return -1;}public static void test4(int... a){int max = a[0];int min = a[0];for(int i = 0; i < a.length; i++){if(max < a[i]){max = a[i];}if(min > a[i]){min = a[i];}}System.out.println(max);System.out.println(min);}public static void test5(int... a){for(int i = a.length - 1; i >= 0; i--){System.out.println(a[i]);}} }9.完成數組的冒泡排序算法
給定一個數組:int[] a = {1,3,2,7,5} 利用冒泡排序對其按照從小到大的順序排序,然后輸出結果。
10.使用第二種算法對數組進行排序
package com.fxm.day05.test; public class Day05Test10{public static void main(String[] args){int[] a = {1,3,2,7,5};for(int i = 0; i < a.length; i++){int m = i;for(int j = i + 1; j < a.length; j++){if(a[j] < a[m]){m = j;}}if(i != m){int temp = a[i];a[i] = a[m];a[m] = temp;}}for(int n = 0; n < a.length; n++){System.out.print(a[n]);}} }11.已知一個二維數組A 表示一個矩陣,求 其中, 表示矩陣的轉置。矩陣轉置的含義:表示把一個矩陣行列互換。
package com.fxm.day05.test; public class Day05Test11{public static void main(String[] args){int[][] a = {{1,2,3},{4,5,6}};int[][] b = new int[3][2];for(int i = 0; i < a.length; i++){for(int j = 0; j < a[i].length; j++){b[j][i] = a[i][j];}}for(int i = 0; i < b.length; i++){for(int j = 0; j < b[i].length; j++){System.out.print(b[i][j]+" ");}System.out.println();}} }總結
- 上一篇: Apollo进阶课程㉓丨Apollo规划
- 下一篇: 0.《Apollo自动驾驶工程师技能图谱