java 数组习题
2.寫一個函數(shù),返回一個整數(shù)數(shù)組的平均值
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.寫一個函數(shù)接受一個整數(shù)數(shù)組a,以及一個整數(shù)n,如果n在數(shù)組中存在,則返回n出現(xiàn)的首 次的下標(biāo);如果不存在,則返回-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.寫一個函數(shù)接受一個數(shù)組,打印輸出數(shù)組中的大值和小值。
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.寫一個函數(shù)接受一個數(shù)組,把這個數(shù)組中所有元素順序進(jìn)行顛倒。
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.將上面所有習(xí)題使用可變長參數(shù)替換數(shù)組實現(xiàn)
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.完成數(shù)組的冒泡排序算法
給定一個數(shù)組:int[] a = {1,3,2,7,5} 利用冒泡排序?qū)ζ浒凑諒男〉酱蟮捻樞蚺判?#xff0c;然后輸出結(jié)果。
10.使用第二種算法對數(shù)組進(jìn)行排序
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.已知一個二維數(shù)組A 表示一個矩陣,求 其中, 表示矩陣的轉(zhuǎn)置。矩陣轉(zhuǎn)置的含義:表示把一個矩陣行列互換。
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();}} }總結(jié)
- 上一篇: Apollo进阶课程㉓丨Apollo规划
- 下一篇: 0.《Apollo自动驾驶工程师技能图谱