Java 如何找到矩阵元素的和
在Java中,数组是一个对象。它是一个非原始数据类型,用于存储相似数据类型的值。Java中的矩阵实际上是一个多维数组,代表了多行和列。
在这里,我们提供了一个包含一组元素的矩阵,根据问题描述,我们需要找出矩阵中所有元素的和。
让我们深入探讨一下这篇文章,了解如何使用Java编程语言来完成它。
展示一些实例
示例1
给定矩阵 =
21 22 23
24 25 26
27 28 29
- 以上矩阵中元素之和=315
示例2
给定矩阵=
121 222 243 432
124 245 256 657
237 258 229 345
176 453 756 343
- 上述矩阵的元素之和=4194
示例3
给定矩阵=
1 2 3
4 5 6
7 8 9
- 上述矩阵的元素总和 = 45
步骤
步骤-1
- 步骤-1 - 声明一个二维矩阵。
-
步骤-2 - 调用用户定义的方法来找到矩阵中所有元素的和。
-
步骤-3 - 该方法使用两个嵌套的循环遍历矩阵中的每个元素,并将当前元素的值添加到一个运行总和中。
-
步骤-4 - 该方法返回矩阵中所有元素的和。
步骤-2
-
步骤-1 - 声明一个二维矩阵。
-
步骤-2 - 调用用户定义的方法来找到矩阵中所有元素的和。
-
步骤-3 - 该方法使用Arrays.stream将二维矩阵转换为一维元素流,并使用forEach将每个元素的值添加到一个运行总和中。
-
步骤-4 - 该方法返回矩阵中所有元素的和。
步骤-3
-
步骤-1 - 声明一个二维矩阵。
-
步骤-2 - 调用用户定义的方法来找到矩阵中所有元素的和。
-
步骤-3 - 该方法使用Stream API将二维矩阵转换为一维元素流,并使用reduce方法来找到流中所有元素的和。
-
步骤-4 - 该方法返回矩阵中所有元素的和。
语法
要获取数组的长度(即数组中的元素数),有一个内置属性,即 长度
下面是其语法参考 –
array.length
在这里,“array”指的是数组引用。
多种方法
我们以不同的方法提供解决方案。
- 使用嵌套循环:
-
使用 Stream API:
-
使用 Arrays.stream 和 flatMap:
让我们逐个查看程序及其输出。
方法1:使用嵌套循环
在这种方法中,程序将初始化矩阵元素。然后,通过将数组作为参数传递给用户定义的方法,并在该方法内部按照算法使用嵌套循环计算给定矩阵的元素之和。
示例
public class Main {
public static void main(String[] args) {
// declare and initialize the 2D matrix
int[][] inputMatrix = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};
// Call the user-defined method to get the sum of all elements
int sum = sumOfMatElmt(inputMatrix);
// Print the result
System.out.println("Sum of matrix elements: " + sum);
}
//user-defined method to find the sum of the elements
public static int sumOfMatElmt(int[][] mat) {
// declare and initialize the sum variable
int sum = 0;
for (int i = 0; i < mat.length; i++) {
// initiate the looping over each column of the current row
for (int j = 0; j < mat[i].length; j++) {
// Add the current element to the sum
sum += mat[i][j];
}
}
// Return the result
return sum;
}
}
输出
Sum of matrix elements: 450
方法2:使用Stream API
在这个方法中,矩阵元素将在程序中被初始化。然后调用一个用户定义的方法,将数组作为参数传递,并在方法内使用流API根据算法计算给定矩阵元素的总和。
示例
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// declare and Initialize the 2D matrix
int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}};
// Call the sumOfMatrixElements method to get the sum of all elements
int sum = sumOfMatElmnt(inputMatrix);
// Print the result
System.out.println("Sum of matrix elements: " + sum);
}
public static int sumOfMatElmnt(int[][] mat) {
// Create an IntStream for each row of the matrix
// and use map to get the sum of each row
// Finally, use sum method to get the sum of all rows
return IntStream.range(0, mat.length)
.map(i -> IntStream.of(mat[i]).sum())
.sum();
}
}
输出
Sum of matrix elements: 495
方法3:使用Arrays.stream和flatMap
在这种方法中,矩阵元素将在程序中进行初始化。然后通过传递数组作为参数来调用一个用户定义的方法,方法内部根据算法使用Arrays.stream和flatMap计算给定矩阵元素的总和。
示例
import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
// Declare a 2D matrix
int[][] matrix = {{12, 23, 34}, {45, 56, 67}, {78, 89, 90}};
// Call the calculateSumOfElements method to find the sum of all elements
int sum = calculateSumOfElements(matrix);
// Print the result
System.out.println("The sum of all elements in the matrix is: " + sum);
}
public static int calculateSumOfElements(int[][] matrix) {
// Use Arrays.stream and flatMap to convert the 2D matrix into a 1D stream of elements
return Arrays.stream(matrix)
.flatMapToInt(IntStream::of)
.sum();
}
}
输出
The sum of all elements in the matrix is: 494
在这篇文章中,我们使用Java编程语言探讨了寻找矩阵和的不同方法。