矩阵元素相加
package test;
import java.util.Arrays;
public class MatrixAddition {
public static void main(String args[]){
matrixElementSumTest();
matrixAdditionTest();
}//Of main
public static int matrixElementSum(int[][] paraMatrix){
//在未标明int[][]内数字时,会预留多大的矩阵空间?已经int为什么后面还要= new int[3][4]
int resultSum = 0;
for (int i = 0; i < paraMatrix.length; i++){
for (int j = 0; j < paraMatrix[0].length; j++){
//paraMatrix[0]代表列数
//为什么不是由i=1开始,直到=paraMatrix结束呢?程序中[0,0]点表示数学中[1,1]?
//既然main中指向的方法是Test,为什么不把matrixAdditionTest放在前?
resultSum += paraMatrix[i][j];
}//Of for j
}//Of for i
return resultSum;
}//Of matrixElementSum
public static void matrixElementSumTest(){
int[][] tempMatrix = new int[3][4];
for(int i = 0; i < tempMatrix.length; i++){
for (int j = 0; j < tempMatrix[0].length; j++){
tempMatrix[i][j] = i * 10 + j;
}//Of for j
}//Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
}//Of matrixElementSumTest
public static int[][] matrixAddition(int[][]paraMatrix1, int[][] paraMatrix2){
int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix1[0].length];
for (int i = 0; i < paraMatrix1.length; i++){
for (int j = 0; j < paraMatrix1[0].length; j++){
resultMatrix[i][j] = paraMatrix1[i][j] + paraMatrix2[i][j];
}//Of for j
}//Of for i
return resultMatrix;
}//Of matrixAddition
public static void matrixAdditionTest(){
int[][] tempMatrix = new int[3][4];
for (int i = 0; i < tempMatrix.length; i++){
for (int j = 0; j < tempMatrix[0].length; j++){
tempMatrix[i][j] = i * 10 + j;
}//Of for j
}//Of for i
System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
}//Of matrixAdditionTest
}//Of class MatrixAddition
question:
1.写程序的顺序是什么?程序最后的样子太有大局观,并不符合搭建逻辑