Java 中查詢矩陣每行的最大元素
在 Java 中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的值。Java 中的矩陣只不過是一個多維陣列,它表示多行多列。
這裡我們給出了一個包含一組元素的矩陣,根據問題陳述,我們必須找出該矩陣中每行的最大元素。
讓我們深入研究本文,瞭解如何使用 Java 程式語言來實現它。
為您展示一些例項
例項 1
給定矩陣 =
21 22 23 24 25 26 27 28 29
每行的最大元素:23、26 和 29
例項 2
給定矩陣 =
121 222 243 432 124 245 256 657 237 258 229 345 176 453 756 343
每行的最大元素:432、657、345 和 756
例項 3
給定矩陣 =
1 2 3 4 5 6 7 8 9
每行的最大元素:3、6 和 9
演算法
演算法 1:(使用 for 迴圈)
步驟 1 - 使用兩個巢狀的 for 迴圈遍歷矩陣並在每行中找到最大元素。
步驟 2 - 外部迴圈遍歷矩陣的每一行,而內部迴圈遍歷該行中的每個元素。
步驟 3 - 使用 Math.max 方法找到行中的最大元素,該方法返回兩個值中較大的一個。
步驟 4 - 結果儲存在一個數組中,並在最後返回。
演算法 2:(使用 Java 流)
步驟 1 - 使用 Java 的流 API 查詢每行的最大元素。
步驟 2 - IntStream.range 方法用於建立從 0 到矩陣行數的整數流。
步驟 3 - map 方法用於將函式 IntStream.of(matrix[i]).max().getAsInt() 應用於流中的每個整數。
步驟 4 - 此函式將行 matrix[i] 中的最大元素作為整數返回。
步驟 5 - 結果儲存在一個數組中,並在最後返回。
語法
Java 中的 Matrix.length() 方法返回給定矩陣的長度。
下面指的是它的語法 -
inputMatrix.lenght
其中,“inputMatrix”指的是給定的矩陣。
IntStream.range() 是 Java 中的一種方法,它生成從 startInclusive 到 endExclusive - 1 的順序整數流。
IntStream.range(startInclusive, endExclusive)
它可用於對一組整數執行操作。例如,在第二個程式碼中,它用於迴圈遍歷矩陣的行和列。
多種方法
我們以不同的方法提供瞭解決方案。
使用 for 迴圈
使用 Stream 方法
讓我們逐一檢視程式及其輸出。
方法 1:使用巢狀 for 迴圈
在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞給使用者定義的方法,並在方法內部根據演算法使用 for 迴圈查詢該矩陣中每行的最大元素。
示例
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] inputMatrix = {{12, 12, 3}, {74, 65, 64}, {57, 28, 49}};
System.out.println("Max element in each row: ");
int[] result = maxInEachRow(inputMatrix);
System.out.println(Arrays.toString(result));
}
// Method to find the maximum element in each row of the matrix
public static int[] maxInEachRow(int[][] mat) {
// Array to store the result
int[] result = new int[mat.length];
// Outer loop to iterate through each row of the matrix
for (int i = 0; i < mat.length; i++) {
// Initialize max to the minimum value of int
int max = Integer.MIN_VALUE;
// Inner loop to iterate through each element in the row
for (int j = 0; j < mat[i].length; j++) {
// Update max with the maximum of the current value and the next element
max = Math.max(max, mat[i][j]);
}
// Store the result in the array
result[i] = max;
}
return result;
}
}
輸出
Max element in each row: [12, 74, 57]
方法 2:使用 Stream 方法
在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞給使用者定義的方法,並在方法內部根據演算法使用流方法查詢該矩陣中每行的最大元素。
示例
import java.util.Arrays;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println("Max element in each row: ");
int[] result = maxInEachRow(matrix);
System.out.println(Arrays.toString(result));
}
// Method to find the maximum element in each row of the matrix using streams
public static int[] maxInEachRow(int[][] matrix){
return IntStream.range(0, matrix.length)
// Get the maximum element in the row
.map(i -> IntStream.of(matrix[i]).max().getAsInt())
// Convert the stream to an array
.toArray();
}
}
輸出
Max element in each row: [3, 6, 9]
在本文中,我們探討了使用 Java 程式語言查詢矩陣中每行最大元素的不同方法。
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP