package First; import java.util.Arrays; public class first { public static void main(String[] args) { matrixElementSumTest(); matrixAdditionTest(); } public static int matrixElementSum(int[][] paraMatrix) { int resultSum = 0; for (int i = 0; i < paraMatrix.length; i++) { for (int j = 0; j < paraMatrix[0].length; j++) { resultSum += paraMatrix[i][j]; } } return resultSum; } 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; } } System.out.println("The matrix is: rn" + Arrays.deepToString(tempMatrix)); System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "rn"); } 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]; } } return resultMatrix; } 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; } } System.out.println("The matrix is: rn" + Arrays.deepToString(tempMatrix)); int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix); System.out.println("The new matrix is: rn" + Arrays.deepToString(tempNewMatrix)); } }单词方面
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)