在Java中查詢矩陣每一列的最大值和最小值?
在Java中,陣列是一個物件。它是一種非基本資料型別,用於儲存相似資料型別的多個值。Java中的矩陣只不過是一個多維陣列,它表示多行和多列。
這裡我們給出一個包含一組元素的矩陣,根據題意,我們需要找出該矩陣每一列的最大值和最小值。
讓我們深入探討這篇文章,瞭解如何使用Java程式語言來實現這一點。
展示一些例項
例項1
給定矩陣 =
21 22 23 24 25 26 27 28 29
第1列中的最大元素是27
第2列中的最大元素是28
第3列中的最大元素是29
第1列中的最小元素是21
第2列中的最小元素是22
第3列中的最小元素是23
例項2
給定矩陣 =
121 222 243 432 124 245 256 657 237 258 229 345 176 453 756 343
第2列中的最大元素是453
第3列中的最大元素是756
第4列中的最大元素是657
第1列中的最小元素是121
第2列中的最小元素是222
第3列中的最小元素是229
第4列中的最小元素是343
例項3
給定矩陣 =
1 2 3 4 5 6 7 8 9
第1列中的最大元素是7
第2列中的最大元素是8
第3列中的最大元素是9
第1列中的最小元素是1
第2列中的最小元素是2
第3列中的最小元素是3
演算法
演算法1:(使用巢狀for迴圈)
步驟1 − 定義一個二維矩陣。
步驟2 − 獲取行數和列數。
步驟3 − 使用巢狀for迴圈查詢每一列中的最大元素。將每一列中的每個元素與當前最大值進行比較,如果找到更大的元素,則更新最大值。
步驟4 − 使用另一個巢狀for迴圈查詢每一列中的最小元素。將每一列中的每個元素與當前最小值進行比較,如果找到更小的元素,則更新最小值。
步驟5 − 列印結果作為輸出。
演算法2:(使用Stream API)
步驟1 − 定義一個二維矩陣。
步驟2 − 獲取列數。
步驟3 − 使用for迴圈和Java Stream API查詢每一列中的最大元素。將每一列中的元素對映到整數流,並在該流中查詢最大值。
步驟4 − 使用for迴圈和Java Stream API查詢每一列中的最小元素。將每一列中的元素對映到整數流,並在該流中查詢最小值。
步驟5 − 列印結果作為輸出。
語法
Java中的Matrix.length()方法返回給定矩陣的長度。
以下是它的語法:
inputMatrix.lenght
其中,“inputMatrix”指的是給定的矩陣。
Arrays.stream(matrix) 將二維整數陣列矩陣轉換為陣列流,其中流中的每個陣列代表矩陣中的一行。
Arrays.stream(matrix)
.mapToInt(row -> row[column]) 將流中的每個陣列(行)對映到指定列索引處的整數。
.mapToInt(row -> row[column])
.max() 返回流中的最大元素。
.max()
.min() 返回流中的最小元素。
.min()
多種方法
我們提供了多種解決方案。
使用巢狀for迴圈
使用Stream API
讓我們逐一檢視程式及其輸出。
方法1:使用巢狀for迴圈
在這種方法中,矩陣元素將在程式中初始化。然後,根據演算法,使用巢狀for迴圈查詢該矩陣每一列中的最大值和最小值。
示例
public class Main {
public static void main(String[] args) {
int[][] inputMatrix = {
{101, 15, 121},
{210, 115, 71},
{81, 215, 118}
};
int r = inputMatrix.length;
int c = inputMatrix[0].length;
//loop to find the largest element in each column
for (int x = 0; x < c; x++) {
int maxm = inputMatrix[0][x];
for (int y = 1; y < r; y++) {
if (inputMatrix[y][x] > maxm) {
maxm = inputMatrix[y][x];
}
}
System.out.println("The largest element in column " + (x+1) + " is " + maxm);
}
//loop to find the smallest elements in each column
for (int x = 0; x < c; x++) {
int mimm = inputMatrix[0][x];
for (int y = 1; y < r; y++) {
if (inputMatrix[y][x] < mimm) {
mimm = inputMatrix[y][x];
}
}
System.out.println("The smallest element in column " + (x+1) + " is " + mimm);
}
}
}
輸出
The largest element in column 1 is 210 The largest element in column 2 is 215 The largest element in column 3 is 121 The smallest element in column 1 is 81 The smallest element in column 2 is 15 The smallest element in column 3 is 71
方法2:使用矩陣的動態初始化
在這種方法中,矩陣元素將在程式中初始化。然後,根據演算法,使用Java流查詢該矩陣每一列中的最大值和最小值。
示例
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Define the matrix
int[][] matrix = {
{1, 5, 3},
{2, 7, 4},
{9, 6, 8}
};
// Get the number of columns
int numCols = matrix[0].length;
// Iterate over each column
for (int i = 0; i < numCols; i++) {
// Create a final variable to hold the current column number
final int column = i;
// Use Java streams to find the largest element in the column
int max = Arrays.stream(matrix)
.mapToInt(row -> row[column])
.max()
.getAsInt();
// Use Java streams to find the smallest element in the column
int min = Arrays.stream(matrix)
.mapToInt(row -> row[column])
.min()
.getAsInt();
// Output the results to the console
System.out.println("---------------");
System.out.println("Column " + (i + 1) + ":");
System.out.println("---------------");
System.out.println("Largest element: " + max);
System.out.println("Smallest element: " + min);
}
}
}
輸出
--------------- Column 1: --------------- Largest element: 9 Smallest element: 1 --------------- Column 2: --------------- Largest element: 7 Smallest element: 5 --------------- Column 3: --------------- Largest element: 8 Smallest element: 3
在這篇文章中,我們探討了使用Java程式語言查詢矩陣每一列中的最大值和最小值的各種方法。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP