day7:矩阵元素相加

一、二维数组的声明形式:

(1)数组类型 数组名[][];

(2)数组类型 []数组名 [];

(3)数组类型 [][] 数组名;

二、二维数组的创建:

new 数据类型[ 行数表达式 ][ 列数表达式 ] 

注意:

①在JAVA中二维数组中的每一行的元素个数可以不同;

②二维数组可以直接初始化,行列数为初始元素个数要确定

③二维数组在创建时有默认值,数值型的初值为0,字符型的初值为空,布尔型的初值为“false”。

三、二维数组元素的访问形式

二维数组名[ 下标1 ][ 下标2 ]

注意:x.length表示二维数组x的行数;

x[ i ].length表示二维数组x的第i行元素的个数。

package test;

import java.util.Arrays;

/**
 * The usage of the matrix.
 * 
 * @author 前夜
 */
public class MatrixAddition {

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args not used now.
	 ********************* 
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		matrixElementSumTest();
		
		matrixAdditionTest();

	}// Of main
	
	/**
	 *********************
	 * Return the 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 for j
		} // Of for i
		
		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;
			} // 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

	/**
	 *********************
	 * 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) {
		//Get the number of rows and columns.获得行数与列数
		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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值