【问题标题】:java How to find the max of each row in a 2d arrayjava如何找到二维数组中每一行的最大值
【发布时间】:2016-08-24 14:33:53
【问题描述】:

我正在编写一个函数,它检索二维数组中每一行的最大值,并返回一个一维数组,其中每个索引都相对于二维数组的列行索引。

例如,如果我有一个二维数组:

{1,2,3}
{4,5,6}
{7,8,9}

它应该返回一个数组

{3,6,9}

到目前为止,这是我的代码:

double[] rowMaxes(double[][] nums) {
    double [] count = new double [nums.length];
    for(int i = 0; i < count.length; i++){
        for(int x = 0; x < nums[0].length; x++){
            for(int n = 0; n < nums.length; n++){
                if(nums[x][n] > count[i]){
                    count[i] = nums[x][n];
                }
            }
        }
    }
    return count;
}

【问题讨论】:

  • 请参考我的Java 8 one liner解决方案

标签: java arrays multidimensional-array compiler-errors max


【解决方案1】:

不需要 3 个嵌套循环。你只需要两个循环:

for(int i = 0; i < count.length; i++){
    for(int x = 0; x < nums[0].length; x++){
        if(nums[i][x] > count[i]){
            count[i] = nums[i][x];
        }
    }
}

【讨论】:

  • 问题是这不适用于作为 2d 数组最大值的负数;仅用于正面
  • @RandyHuang 如果您的数组可能包含负数,您应该将count 数组的所有元素初始化为 Integer.MIN_VALUE
  • @RandyHuang 在第一个循环中缺少count[i] = nums[i][0];
【解决方案2】:

您应该在进入循环之前找到行和列的长度。 如果要考虑负数,则首先将 max 定义为最小负值。 你可以用这个

public static void main(String[] args) {
        double count[][] = {{1,2,3,8},{4,6,5,9},{0,8,9,1}};
        int r = count.length;
        int c= count[0].length;
        double out[] = new double[r];
        for(int i = 0; i < r; i++){
            double max = Integer.MIN_VALUE;
            for(int x = 0; x < c; x++){
                if(count[i][x] > max)
                    max = count[i][x];
            }
            out[i] = max;
        }
        for(int i=0;i<r;i++)
            System.out.println(out[i]);

    } 

【讨论】:

    【解决方案3】:
    public static int[] getMaxOfRow(int arr[][]){
        int grtr[]=new int[arr.length];
        grtr[0]=arr[0][0];
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr[0].length;j++){
                if(arr[i][j]>grtr[i]){
                    grtr[i]=arr[i][j];
                }
            }
        }
        return grtr;
    }                                                                                                
    

    【讨论】:

    【解决方案4】:

    请注意,如果一行中的所有值都小于零,您的代码将无法运行。
    当您创建一个新数组时,它将填充默认值 - 它为零。
    因为您需要在第一个循环中添加count[i] = nums[i][0]

    类似的东西

    double[] rowMaxes(double[][] nums) {
        double[] count = new double[nums.length];
    
        for (int i = 0; i < nums.length; i++) {
            count[i] = nums[i][0];
            for (int j = 1; j < nums[i].length; j++) {
                if (count[i] < nums[i][j]) {
                    count[i] = nums[i][j];
                }
            }
        }
    
        return count;
    }
    

    如果你使用 Java 8,你可以用 stream 和 max 方法替换内部循环。

    for (int i = 0; i < nums.length; i++) {
        count[i] = Arrays.stream(nums[i]).max().getAsDouble();
    }
    

    【讨论】:

      【解决方案5】:

      如果您更喜欢一行,这是任何显式循环的解决方案:

      Arrays.stream(nums).mapToInt((row) -> Arrays.stream(row).max().getAsInt()).max().getAsInt()
      

      【讨论】:

        【解决方案6】:
        package com.vikas.array;
        
        public class ArrayGreater_2ndApproach {
        
            public static void main(String[] args) {
        
                int d[][] = { { 1, 5, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        
                for (int i = 0; i < d.length; i++) {
                    int max = 0;
                    for (int j = 0; j < d.length; j++) {
                        if(max<d[i][j]) {
                            max = d[i][j] ;
        
                        }
                    }
                    
                    System.out.println("Max number in the row : " + max);
                }
        
            }
        }
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        • 请仔细阅读问题。问题是关于如何获得最大每行。另外,添加对您的解决方案的评论以分享您的想法。
        猜你喜欢
        • 2018-09-25
        • 2022-11-01
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 2018-09-14
        • 2021-02-18
        • 2016-03-19
        • 1970-01-01
        相关资源
        最近更新 更多