首页  >  文章  >  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删除