日撸java_day07

第 7 天: 矩阵元素相加

7.1 矩阵的赋值.
7.2 二重循环.

package basic;

import java.util.Arrays;

/**
 * ClassName: MatrixAddition
 * Package: basic
 * Description:
 *
 * @Author: luv_x_c
 * @Create: 2023/4/10 9:35
 */
public class MatrixAddition {
    /**
     * The entrance of the program.
     *
     * @param args Not used now.
     */
    public static void main(String[] args) {
        matrixAdditionTest();
    } // Of main

    /**
     * Sum the elements of a matrix.
     *
     * @param paraMatrix The given matrix.
     * @return The sum of all its elements.
     */
    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];
            } // Of j
        } // Of i

        return resultSum;
    } // Of matrixElementSum

    /**
     * Unit test for respective method.
     */
    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 j
        } // Of i

        System.out.println("The matrix is : \r\n" + Arrays.deepToString(tempMatrix));
        System.out.println("The matrix elements sum is: " + matrixElementSum(tempMatrix) + "\r\n");
    } // Of matrixElementSumTest

    /**
     * Addition two matrix. Attention: NO error check is provided at this moment.
     *
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix,it should have the same size as the first one.
     * @return The addition of these matrix.
     */
    public static int[][] matrixAddition(int[][] paraMatrix1, int[][] paraMatrix2) {
        int[][] resultMatrix = new int[paraMatrix1.length][paraMatrix2[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 j
        } // Of 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 j
        } // Of 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

日撸java_day07_第1张图片

第一遍运行错了,数组下标访问越界,

日撸java_day07_第2张图片定位到67行后发现代码写成了这样:

日撸java_day07_第3张图片

 是因为上面定义结果矩阵时候写错了列数,应该是paraMatrix2的列长,

日撸java_day07_第4张图片

1. 数组的使用中常见的异常小结

> 数组角标越界的异常:ArrayIndexOutOfBoundsException
> 空指针的异常:NullPointerException


2. 出现异常会怎样?如何处理?
> 一旦程序执行中出现了异常,程序就会终止执行。
> 针对异常提供的信息,修改对应的代码,避免异常再次出现。

 

 

你可能感兴趣的:(java,算法,开发语言)