public class 数组_二维数组 {
//找出数组中最大的元素
private static Double max(int[] a) {
double max = a[0];
//如果after > before,则将大的这个值赋值给上一个数
for (int i = 1; i < a.length; i++) if (a[i] > max) max = a[i];
return max;
}
//计算数组元素的平均值
private static Double mean(int[] a) {
int N = a.length;
double sum = 0.0;
for (int anA : a) sum += anA;
return sum / N;
}
//复制数组
private static double[] copyArray(int[] a) {
int N = a.length;
//新建一个数组长度是,原数组的长度
double[] b = new double[N];
//将原数组,赋值给新数组,且下标是对应的关系
for (int i = 0; i < N; i++) b[i] = a[i];
return b;
}
//颠倒数组元素的顺序
private static int[] reverse_Order(int[] a) {
int N = a.length;
for (int i = 0; i < N / 2; i++) {
int temp = a[i];
a[i] = a[N - 1 - i];
a[N - i - 1] = temp;
}
return a;
}
/**
* 矩阵相乘
* a[][] * b[][] = c[][]
*/
private static double[][] matrix_Multiplication() {
int[][] a = {{3, 2}, {1, 3}};
int[][] b = {{2, 3}, {1, 1}};
/**
*
* 二维数组a可以看作是一个矩阵
* 3,2
* 1,3
*
* a[0][0]:代表的是第一行第一列:3
* a[0][1]:代表的是第一行第二列:2
* a[1][0]:代表的是第二行第一列:1
* a[1][1]:代表的是第二行第二列:3
* */
int N = a.length;
double[][] c = new double[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
//计算行i和列j的点乘
for (int k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
return c;
}
//二维数组
private static int[][] two_dimensional_array() {
//M*N
int[][] ss = new int[2][3];
//初始化二维数组,第一次循环循环的是M(行)
for (int i = 0; i < ss.length; i++) {
//的二次循环循环的是N(列)
for (int j = 0; j < ss.length; j++) {
ss[i][j] = 0;
}
}
return ss;
}
public static void main(String[] args) {
int[] a = {1, 3, 12, 3, 5};
System.out.println(max(a));
System.out.println(mean(a));
System.out.println(Arrays.toString(copyArray(a)));
//将数组元素进行颠倒
System.out.println("元素颠倒:\t"+Arrays.toString(reverse_Order(a)));
// System.out.println(Arrays.toString(a));
//矩阵相乘
System.out.println(Arrays.deepToString(matrix_Multiplication()));
//二维数组
System.out.println(Arrays.deepToString(two_dimensional_array()));
}
}