一些典型的数组处理方法 赋值初始化 寻找最大值 计算平均值 复制数组 颠倒数组中元素的顺序 矩阵相乘 数组的输出
Posted 昇陽特区
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一些典型的数组处理方法 赋值初始化 寻找最大值 计算平均值 复制数组 颠倒数组中元素的顺序 矩阵相乘 数组的输出相关的知识,希望对你有一定的参考价值。
数组的初始化使用随机数赋值需要使用java.util.Random
//用0-100的数据初始化数组 public static void randomArry(double a[]) { Random ran = new Random(); int n = a.length; for(int i = 0;i <n ;i++) { a[i] = ran.nextInt(100); } } //初始化的重载 public static void randomArry(double a[][]) { Random ran = new Random(); int n1 = a.length; int n2 = a[0].length; for(int i = 0;i<n1;i++) { for(int j = 0;j < n2;j++) { a[i][j] = ran.nextInt(100); } } }
寻找最大值
//找出数组的最大值 public static double findMax(double a[]) { double max = a[0]; for(int i = 1;i < a.length;i++) { if(a[i] >max) max = a[i]; } return max; } //找最大值方法要创建一个对应的重载方法 public static double findMax(double a[][]) { int n1 = a.length; int n2 = a[0].length; double max = a[0][0]; for(int i = 0; i < n1;i++) { for(int j = 0; j < n2;j++) { if(a[i][j] >max) max = a[i][j]; } } return max; }
计算数组元素的平均值
//计算数组元素的平均值 public static double figureAverage(double a[]) { int n = a.length; double sum = 0; for(int i = 0; i < n;i++) { sum += a[i]; } return sum/n; } //当然我们算平均值也要算二维数组了看我们的重载方法如下 public static double figureAverage(double a[][]) { int n1 = a.length; int n2 = a[0].length; double sum = 0; for(int i = 0; i < n1;i++) { for(int j = 0; j < n2;j++) { sum += a[i][j]; } } return sum/(n1*n2); }
复制数组
//复制数组 public static double[] copyArry(double a[]) { int n = a.length; double b[] = new double[n]; for(int i = 0; i < n;i++) { b[i] = a[i]; } return b; } public static double[][] copyArry(double a[][]){ int n1 = a.length; int n2 = a[0].length; double[][] b = new double[n1][n2]; for(int i = 0; i<n1; i++) { for(int j = 0; j < n2; j++) { b[i][j] = a[i][j]; } } return b; }
颠倒数组元素的顺序
//颠倒数组元素的顺序 public static double[] switchElements(double a[]) { int n = a.length; for(int i = 0; i< n/2;i++) { double temp = a[i]; a[i] = a[n - 1 -i]; a[n-1-i] = temp; } return a; } //颠倒数组顺序的重载方法 public static double[][] switchElements(double a[][]){ int n1 = a.length; int n2 = a[0].length; for(int i =0;i<n1/2;i++) { for(int j = 0;j<n2;j++) { if(i+j < (n1+n2)/2) { double temp = a[i][j]; a[i][j] = a[n1-i-1][n2-j-1]; a[n1-i-1][n2-j-1] = temp; } } } return a; }
矩阵相乘
//矩阵相乘 public static double[] matrixMuiplication(double a[],double b[]) { int n = a.length; double[] c = new double[n]; for(int i = 0;i<n;i++) { c[i] = a[i]*b[i]; } return c; } //矩阵相乘的重载(方阵) public static double[][] matrixMuiplication(double a[][],double b[][]) { 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; }
打印数组
//输出数组的方法 public static void printArry(double a[]) { int n = a.length; for(int i = 0; i<n ;i++) { System.out.print(a[i]+"\\t"); } System.out.print("\\n"); System.out.print("\\n"); } //数组输出的重载方法 public static void printArry(double a[][]) { int n1 = a.length; int n2 = a[0].length; for(int i = 0; i<n1 ; i++) { for(int j= 0;j<n2;j++) { System.out.print(a[i][j]); System.out.print("\\t"); } System.out.print("\\n"); } System.out.print("\\n"); }
完整代码带测试
1 import java.util.Random; 2 3 public class DianXingShuZhuChuLiDaiMa { 4 5 6 public static void main(String[] args) { 7 // 接下来对我们的数组方法逐一测试 8 double a[] = new double[8]; 9 double b[] = new double[8]; 10 double b1[][] = new double[8][8]; 11 double b2[][] =new double[8][8]; 12 printArry(a); 13 printArry(b); 14 printArry(b1); 15 printArry(b2); 16 //使用随机数来作为我们测试的量 17 randomArry(a); 18 randomArry(b); 19 randomArry(b1); 20 randomArry(b2); 21 //输出初始化的值 22 System.out.println("最初的a是"); 23 printArry(a); 24 System.out.println("最初的b是"); 25 printArry(b); 26 System.out.println("最初的b1是"); 27 printArry(b1); 28 System.out.println("最初的b2是"); 29 printArry(b2); 30 31 //找出a的最大值找出b2的最大值 32 System.out.println("a的最大值是:"+findMax(a)); 33 System.out.println("b2的最大值是:"+findMax(b2)); 34 //接下来计算数组元素的平均值 35 System.out.println("a的平均值是"+figureAverage(a)); 36 System.out.println("b1的平均值是"+figureAverage(b1)); 37 //复制数组 38 double d1[] = copyArry(a); 39 double d2[][] = copyArry(b1); 40 System.out.println("复制结果是"); 41 printArry(d1); 42 printArry(d2); 43 //颠倒数组的元素顺序 44 System.out.println("颠倒数组的结果是a & b1"); 45 printArry(switchElements(a)); 46 printArry(switchElements(b1)); 47 48 //测试矩阵相乘 49 System.out.println("ab相乘"); 50 printArry(matrixMuiplication(a,b)); 51 System.out.println("b1 b2相乘"); 52 printArry(matrixMuiplication(b1,b2)); 53 54 55 } 56 57 //用0-100的数据初始化数组 58 public static void randomArry(double a[]) { 59 Random ran = new Random(); 60 int n = a.length; 61 for(int i = 0;i <n ;i++) { 62 a[i] = ran.nextInt(100); 63 } 64 } 65 //初始化的重载 66 public static void randomArry(double a[][]) { 67 Random ran = new Random(); 68 int n1 = a.length; 69 int n2 = a[0].length; 70 for(int i = 0;i<n1;i++) { 71 for(int j = 0;j < n2;j++) { 72 a[i][j] = ran.nextInt(100); 73 74 } 75 } 76 } 77 78 //找出数组的最大值 79 public static double findMax(double a[]) { 80 double max = a[0]; 81 for(int i = 1;i < a.length;i++) { 82 if(a[i] >max) max = a[i]; 83 } 84 return max; 85 } 86 //找最大值方法要创建一个对应的重载方法 87 public static double findMax(double a[][]) { 88 int n1 = a.length; 89 int n2 = a[0].length; 90 double max = a[0][0]; 91 for(int i = 0; i < n1;i++) { 92 for(int j = 0; j < n2;j++) { 93 if(a[i][j] >max) max = a[i][j]; 94 } 95 } 96 return max; 97 } 98 99 //计算数组元素的平均值 100 public static double figureAverage(double a[]) { 101 int n = a.length; 102 double sum = 0; 103 for(int i = 0; i < n;i++) { 104 sum += a[i]; 105 } 106 return sum/n; 107 } 108 //当然我们算平均值也要算二维数组了看我们的重载方法如下 109 public static double figureAverage(double a[][]) { 110 int n1 = a.length; 111 int n2 = a[0].length; 112 double sum = 0; 113 for(int i = 0; i < n1;i++) { 114 for(int j = 0; j < n2;j++) { 115 sum += a[i][j]; 116 } 117 } 118 return sum/(n1*n2); 119 } 120 121 //复制数组 122 public static double[] copyArry(double a[]) { 123 int n = a.length; 124 double b[] = new double[n]; 125 for(int i = 0; i < n;i++) { 126 b[i] = a[i]; 127 } 128 return b; 129 } 130 public static double[][] copyArry(double a[][]){ 131 int n1 = a.length; 132 int n2 = a[0].length; 133 double[][] b = new double[n1][n2]; 134 for(int i = 0; i<n1; i++) { 135 for(int j = 0; j < n2; j++) { 136 b[i][j] = a[i][j]; 137 } 138 } 139 return b; 140 } 141 142 //颠倒数组元素的顺序 143 public static double[] switchElements(double a[]) { 144 int n = a.length; 145 for(int i = 0; i< n/2;i++) { 146 double temp = a[i]; 147 a[i] = a[n - 1 -i]; 148 a[n-1-i] = temp; 149 } 150 return a; 151 } 152 //颠倒数组顺序的重载方法 153 public static double[][] switchElements(double a[][]){ 154 int n1 = a.length; 155 int n2 = a[0].length; 156 for(int i =0;i<n1/2;i++) { 157 for(int j = 0;j<n2;j++) { 158 if(i+j < (n1+n2)/2) { 159 double temp = a[i][j]; 160 a[i][j] = a[n1-i-1][n2-j-1]; 161 a[n1-i-1][n2-j-1] = temp; 162 } 163 } 164 } 165 return a; 166 } 167 168 //矩阵相乘 169 public static double[] matrixMuiplication(double a[],double b[]) { 170 int n = a.length; 171 double[] c = new double[n]; 172 for(int i = 0;i<n;i++) { 173 c[i] = a[i]*b[i]; 174 } 175 return c; 176 } 177 //矩阵相乘的重载(方阵) 178 public static double[][] matrixMuiplication(double a[][],double b[][]) { 179 int n = a.length; 180 double c[][] = new double[n][n]; 181 for(int i = 0;i < n;i++) { 182 for(int j = 0; j<n; j++) { 183 //计算行i和列j的点乘 184 for(int k = 0; k < n; k++) { 185 c[i][j] += a[i][k]*b[k][j]; 186 } 187 } 188 } 189 return c; 190 } 191 //输出数组的方法 192 public static void printArry(double a[]) { 193 int n = a.length; 194 for(int i = 0; i<n ;i++) { 195 System.out.print(a[i]+"\\t"); 196 } 197 System.out.print("\\n"); 198 System.out.print("\\n"); 199 } 200 //数组输出的重载方法 201 public static void printArry(double a[][]) { 202 int n1 = a.length; 203 int n2 = a[0].length; 204 for(int i = 0; i<n1 ; i++) { 205 for(int j= 0;j<n2;j++) { 206 System.out.print(a[i][j]); 207 System.out.print("\\t"); 208 } 209 System.out.print("\\n"); 210 } 211 System.out.print("\\n"); 212 } 213 }
输出结果
以上是关于一些典型的数组处理方法 赋值初始化 寻找最大值 计算平均值 复制数组 颠倒数组中元素的顺序 矩阵相乘 数组的输出的主要内容,如果未能解决你的问题,请参考以下文章