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
输出:
小问题:
\r为回车符,让光标回到一行的第一个位置,如果后面有字符会被覆盖。
\n为换行符,让光标去到下一行的存在字符的下一个位置(若下一行为空则第一个位置)。
一般在运用\n与\r\n时输出结果没有什么影响,是因为输出的下一行一般都为空。