首頁  >  文章  >  Java  >  在Java中將矩陣中的負數和正數替換為0和1

在Java中將矩陣中的負數和正數替換為0和1

王林
王林轉載
2023-08-29 08:29:081108瀏覽

在Java中將矩陣中的負數和正數替換為0和1

在Java中,陣列是一個物件。它是一種非原始資料類型,儲存類似資料類型的值。 java中的矩陣只不過是一個表示多行和多列的多維數組。

這裡我們給出了一個矩陣,其中包含一組元素,其中包括正數和負數,根據問題陳述,我們必須將負數替換為 0,將正數替換為 1。

讓我們深入研究這篇文章,了解如何使用 Java 程式語言來完成它。

向您展示一些實例

實例1

給定矩陣=

-21 	22	-23
24	-25	26
-27	-28	29

將負數替換為 0,正數替換為 1 後,

所得矩陣為 -

0 	1	0
1	0	1
0	0	1

實例2

給定矩陣=

-9 	2	-2	4
-1	-7	-2	6
2	-2	-4	3
-1	4	7	-8

將負數替換為 0,正數替換為 1 後,

所得矩陣為 -

0 	1	0	1
0	0	0	1
1	0	0	1
0	1	1	0

實例3

給定矩陣 =

-1 	-2	-3
4	5	6
-7	8	-9

將負數替換為 0,正數替換為 1 後,

所得矩陣為:-

0 	0	0
1	1	1
0	1	0

演算法

演算法1

  • 第 1 步 - 建立一個 2D 陣列矩陣來儲存數字。

  • Step-2 - 呼叫replaceNum方法將矩陣中的負數替換為0,將正數替換為1。

  • Step-3 - 列印結果矩陣。

  • Step-4 - 在replaceNum方法中,使用for迴圈迭代矩陣的行和列。

  • Step-5 - 對於矩陣中的每個元素,使用三元運算子將數字替換為 0 或 1,取決於它是負數還是正數。

演算法2

  • 第 1 步 - 建立一個 2D 陣列矩陣來儲存數字。

  • Step-2 - 呼叫replaceNum方法將矩陣中的負數替換為0,將正數替換為1。

  • Step-3 - 列印結果矩陣。

  • Step-4 - 在replaceNum方法中,使用for迴圈迭代矩陣的行和列。

  • Step-5 - 對於矩陣中的每個元素,使用 Math.signum 方法確定數字的符號(-1 表示負數,0 表示 0,1 表示正面的)。然後使用 if-else 語句將數字替換為 0 或 1,取決於它是負數還是正數

文法

要取得數組的長度(該數組中元素的數量),數組有一個內建屬性,即 length

下面是它的語法 -

array.length

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

Java 中的Math.signum() 方法是一個數學函數,它會傳回給定雙精確度或浮點值的符號(-1 表示負數,0 表示0,1 表示正數) 。

下面是它的語法 -

Math.signum(mat[a][b]) == -1.0)

其中,‘mat’指的是給定的矩陣。

多種方法

我們透過不同的方式提供了解決方案。

  • 透過使用三元運算子

  • 透過使用 Math.signum 函數

讓我們一一看看該程式及其輸出。

方法 1:使用三元運算子

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為參數傳遞來呼叫使用者定義的方法,並根據演算法使用三元運算子將負數替換為 0,將正數替換為 1。

範例

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{-1, 2, -3},
         {4, -5, 6},
         {7, 8, -9}};
      
      // Call the User-defined method to replace negative and positive numbers
      replacenum(inputMatrix);
      
      // Print the resultant matrix
      for (int a = 0; a < inputMatrix.length; a++) {
         for (int b = 0; b < inputMatrix[a].length; b++) {
            System.out.print(inputMatrix[a][b] + " ");
         }
         System.out.println();
      }
   }
   
   // User-defined method to replace numbers 
   public static void replacenum(int[][] mat) {
      for (int a = 0; a < mat.length; a++) {
         for (int b = 0; b < mat[a].length; b++) {
            
            // Use a ternary operator to replace the number
            mat[a][b] = mat[a][b] < 0 ? 0 : 1;
         }
      }
   }
}

輸出

0 1 0 
1 0 1 
1 1 0

方法 2:使用 Math.signum 函數

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為參數傳遞來呼叫使用者定義的方法,並根據演算法使用 Math.signum 方法在方法內部將負數替換為 0,將正數替換為 1。

範例

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{-1, -2, 3},
         {4, -5, -6},
         {7, 8, 9}};
      
      // Call the User-defined method to replace negative and positive numbers
      replaceNum(inputMatrix);
      
      // Print the resultant matrix
      for (int a = 0; a < inputMatrix.length; a++) {
         for (int b = 0; b < inputMatrix[a].length; b++) {
            System.out.print(inputMatrix[a][b] + " ");
         }
         System.out.println();
      }
   }
   
   // User-defined method to replace numbers 
   public static void replaceNum(int[][] mat) {
      for (int a = 0; a < mat.length; a++) {
         for (int b = 0; b < mat[a].length; b++) {
            
            // Use an if-else statement with Math.signum method to replace the number
            if (Math.signum(mat[a][b]) == -1.0) {
               mat[a][b] = 0;
            } else {
               mat[a][b] = 1;
            }
         }
      }
   }
}

輸出

0 0 1 
1 0 0 
1 1 1

在本文中,我們探索了使用 Java 程式語言在矩陣中以 0 取代負數和以 1 取代正數的不同方法。

以上是在Java中將矩陣中的負數和正數替換為0和1的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除