日撸Java三百行 day07(矩阵元素相加)

文章介绍了Java中数组的定义和操作,包括一维和二维数组的动态及静态初始化。提到了Javautil包中的Arrays类,及其常用方法如排序、复制和填充。此外,文章提供了一个矩阵元素相加的示例,包括一个计算矩阵元素总和的函数和矩阵加法的实现。
摘要由CSDN通过智能技术生成

1. Java中的数组

1.1 定义

一维数组:

dataType[] arrayName = new dataType[arraySize]; // 动态初始化,数组中默认值为0
dataType[] arrayName = new dataType[arraySize]{data1, data2, data3, ......}; //静态初始化

二维数组:

dataType[][] arrayName = new dataType[N][M];// 动态初始化

dataType[][] arrayName = new dataType[N][M]{{data1, data2,......, dataM},......, 
    {data1, data2,......, dataM}}; //静态初始化

其中new为动态申请空间的函数(同C++)

可使用arrayName.length来直接获取二维数组中一维数组的个数(行号),arrayName[0].length来获取二维数组中的一维数组中有几个元素(列号)。

1.2 Arrays类

在JDK中提供了一个专门操作数组的工具,即位于java util中的Arrays类。

Arrays类的常有方法有:

toString (array)                         一维数组转换为字符串形式

deepToString (array)                多维数组转换为字符串形式

sort (array)                               对数组进行升序排序(双轴快速排序)

copyOf (array, length)              复制array数组为一个长度为length的新数组

binarySearch (array, value)      查找value的下标(二分查找)

fill (array, value)                        将数组中的全部元素赋为value

equals (array1, array2)             判断两数组是否相等

2. 矩阵元素相加示例

package basic;

import java.util.Arrays; // 导入Arrays类

/**
 * This is the seventh code.
 * 
 * @author Yunhua Hu yunhuahu0528@163.com.
 */
public class MatrixAddition {

	/**
	 *********************
	 * The entrance of the program.
	 *
	 * @param args Not uesd now.
	 *********************
	 */
	public static void main(String args[]) {
		matrixElementSumTest();

		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 for j
		} // Of for 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 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 sth
	 *********************
	 */
	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

	/**
	 *********************
	 * 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 class MatrixAddition

输出:

487deeafdbb647a48ce216b5d08c737b.png

小问题:

\r为回车符,让光标回到一行的第一个位置,如果后面有字符会被覆盖。

\n为换行符,让光标去到下一行的存在字符的下一个位置(若下一行为空则第一个位置)。

一般在运用\n与\r\n时输出结果没有什么影响,是因为输出的下一行一般都为空。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值