如何在 Java 中查詢矩陣每一行的最小元素?


在 Java 中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的多個值。Java 中的矩陣只不過是一個多維陣列,它表示多行和多列。

根據問題陳述,我們需要找到給定多維陣列矩陣每一行中存在的最小元素。

讓我們深入探討本文,瞭解如何使用 Java 程式語言來實現這一點。

舉幾個例子

例項 1

Suppose the original matrix is = {
   {2, 3,11, 4, 6},
   {21,12,32,45,2},
   {41,12,3,45, 2}
}

第 0 行中存在的最小元素是 1

第 1 行中存在的最小元素是 2

第 2 行中存在的最小元素是 2

例項 2

Suppose the original matrix is = {
   {2, 3, 1, 4, 6},
   {21, 12, 32, -4, 2},
   {4, 12, 3, 45, 2},
   {-12, -13, 6, 1, 8}
}

第 0 行中存在的最小元素是 1

第 1 行中存在的最小元素是 -4

第 2 行中存在的最小元素是 2

第 3 行中存在的最小元素是 -13

例項 3

Suppose the original matrix is = {
   {2, 3, 1},
   21,12,32},
   {4,12,13}
}

第 0 行中存在的最小元素是 1

第 1 行中存在的最小元素是 12

第 2 行中存在的最小元素是 4

演算法

步驟 1 − 宣告並初始化一個整數多維陣列。

步驟 2 − 使用迴圈迭代給定矩陣的行。

步驟 3 − 在該迴圈內再使用一個 for 迴圈來迭代列,透過這種方法,我們現在可以檢查行中所有可用的元素。

步驟 4 − 我們將這些行的元素傳遞到條件中以獲取最小值。

步驟 5 − 在每個迴圈中獲取最小值後,我們將這些值及其對應的行索引號作為輸出列印。

語法

要獲取陣列的長度(陣列中元素的數量),陣列有一個內建屬性,即length

下面是它的語法:

array.length

其中,'array' 指的是陣列引用。

多種方法

我們提供了不同的方法來解決這個問題。

  • 使用陣列元素的靜態初始化

  • 使用使用者自定義方法

讓我們逐一檢視程式及其輸出。

方法 1:使用陣列元素的靜態初始化

在這種方法中,陣列元素將在程式中初始化。然後根據演算法,矩陣每一行的最小值將作為輸出列印。

示例

public class Main{
   public static void main(String[] args) {
      
      //declare an integer type multi dimentional array
      //here we take a 3X3 Matrix with some random values
      int[][] inputMatrix = {
         {21,34,76},
         {2,1,5},
         {90,23,-23}
      };
      for (int r = 0; r < inputMatrix.length; r++) {
         int minimumValue = inputMatrix[r][0];
         for (int c = 0; c < inputMatrix[r].length; c++){
            
            //if the current index value is smaller than the Minimum value
            if (inputMatrix[r][c] < minimumValue) {
               
               //store the minimum value to the variable each time 
               //when the above condition satisfied
               minimumValue = inputMatrix[r][c];
            }
         }
         
         //print the row index value along with the derived minimum value
         System.out.println("The minimum element present in row-" + r + " is " + minimumValue);
      }
   }
}

輸出

The minimum element present in row-0 is 21
The minimum element present in row-1 is 1
The minimum element present in row-2 is -23

方法 2:使用使用者自定義方法

在這種方法中,陣列元素將在程式中初始化。然後透過將陣列作為引數呼叫使用者自定義方法,並在方法內部根據演算法,矩陣每一行的最小值將作為輸出列印。

示例

public class Main {
   public static void main(String[] args) {
      
      //declare an integer type multi dimentional array
      //a 3X3 Matrix with some random values
      int[][] inputMatrix ={
         {2, 3, 1}, 
         {21,12,32}, 
         {4,12,3}
      };
     
     //call the user-defined method
      Min(inputMatrix);
   }
   //user defined method
   public static void Min(int[][] mat) {
      for (int r = 0; r < mat.length; r++) {
         int minimumValue = mat[r][0];
         
         //initiate the loop to check through the columns
         for (int c = 0; c < mat[r].length; c++){
           
           //if the current index value is smaller than the Minimum value
            if (mat[r][c] < minimumValue) {
              
               //store the minimum value to the variable each time 
               //when the above condition satisfied
               minimumValue = mat[r][c];
            }
         }
        
         //print the row index value along with the derived minimum value
         System.out.println("The minimum element present in row-" + r + " is " + minimumValue);
      }
   }
}

輸出

The minimum element present in row-0 is 1
The minimum element present in row-1 is 12
The minimum element present in row-2 is 3

在本文中,我們探索了使用 Java 程式語言查詢矩陣每一行中最小元素的不同方法。

更新於: 2023 年 3 月 6 日

1K+ 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.