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} 利用冒泡排序对其按照从小到大的顺序排序,然后输出结果。
package com.fxm.day05.test;
public class Day05Test9{public static void main(String[] args){int[] a = {1,3,2,7,5};for(int i = 1; i < a.length; i++){for(int j = 0; j < a.length-i; j++){if(a[j] > a[j+1]){int temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}for(int n = 0; n < a.length; n++){System.out.print(a[n]);}}
}
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();}}
}