java学习day6-day7

100 阅读2分钟

day6 基本for 语句

6.1 for语句中表达式的执行顺序

for(a;b;c)其中a,b,c为表达式,执行顺序:先执行a表达式,一般为初始化语句,再执行b表达式,一般式判断表达式,若为ture去执行循环体,执行完再执行c表达式,若不满足b表达式,则跳出循环。

6.2 代码

package basic;
public class ForStatement {
    /**
     * The entrance of the program.
     * @param args
     */
    public static void main(String[] args) {
        forStatementTest();
    }

    /**
     * Method unit test.
     */
    public static void forStatementTest(){
        int tempN = 0;
        System.out.println("1 add to " + tempN + " is: " + addToN(tempN));

        tempN = 0;
        System.out.println("1 add to " + tempN + " is: " + addToN(tempN));

        int tempStepLength = 1;
        tempN = 10;
        System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
                + addToNWithStepLength(tempN, tempStepLength));

        tempStepLength = 2;
        System.out.println("1 add to " + tempN + " with step length " + tempStepLength + " is: "
                + addToNWithStepLength(tempN, tempStepLength));
    }

    /**
     * Add from 1 to N.
     * @param paraN The given upper bound.
     * @return The sum.
     */
    public static int addToN(int paraN) {
        int resultSum = 0;

        for (int i = 1; i <= paraN; i++) {
            resultSum += i;
        }

        return resultSum;
    }

    /**
     * Add from 1 to N with a step length.
     * @param paraN The given upper bound.
     * @param paraStepLength paraStepLength The given step length.
     * @return The sum.
     */
    public static int addToNWithStepLength(int paraN, int paraStepLength) {
        int resultSum = 0;

        for (int i = 1; i <= paraN; i += paraStepLength) {
            resultSum += i;
        }

        return resultSum;
    }
}

day7 矩阵元素相加

7.1 题目解读

矩阵用二维数组存储,计算二维数组的和,计算两个二维数组对应行列相加组成一个新的二维数组,都需要用到for循环遍历(行优先);对矩阵的赋值也需要循环遍历赋初值,在有循环时要避免死循环,确保循环是有限性的。

7.2 二维数组中

int[][] tempMatrix = new int[3][4];
tempMatrix.length; //代表行的长度
tempMatrix[0].length; //代表列的长度

代码:

package basic;

import java.util.Arrays;
public class MatrixAddition {
    public static void main(String[] args) {
        matrixElementSumTest();

        matrixAdditionTest();
    }

    /**
     * Sum the elements of a matrix.
     * @param paraMatrix
     * @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];
            }
        }

        return resultSum;
    }

    /**
     * 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;
            }
        }

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

    /**
     * Add two matrices. 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's
     * @return The addition of these matrices.
     */
    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;
    }

    /**
     * Unit test for respective method.
     */
    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: \r\n" + Arrays.deepToString(tempMatrix));
        int[][] tempNewMatrix = matrixAddition(tempMatrix, tempMatrix);
        System.out.println("The new matrix is: \r\n" + Arrays.deepToString(tempNewMatrix));
    }
}