Java 查找矩阵每行的最大元素

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 - 此函数将矩阵[i]中的最大元素作为整数返回。

  • 步骤5 - 结果存储在一个数组中,并在结束时返回该数组。

语法

Java中的Matrix.length()方法返回给定矩阵的长度。

下面是它的语法-

inputMatrix.lenght

其中,’inputMatrix’指的是给定的矩阵。

IntStream.range() 是Java中的一个方法,它生成一个从startInclusive到endExclusive – 1的连续整数流。

IntStream.range(startInclusive, endExclusive)

它可以用来对一组整数执行操作。例如,在第二段代码中,它被用来循环遍历矩阵的行和列。

多种方法

我们提供了不同的方法来解决问题。

  • 使用for循环

  • 使用流方法

让我们依次看看程序及其输出。

方法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:使用流方法

在这种方法中,矩阵元素将在程序中进行初始化。然后通过将矩阵作为参数调用用户定义的方法,并在方法内根据算法使用流方法找到该矩阵每行的最大元素。

示例

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编程语言探索了不同的方法来查找矩阵每行的最大元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程