Java 如何从2d数组中的所有行中获取最大数量的和值的索引

Java 如何从2d数组中的所有行中获取最大数量的和值的索引,java,Java,这是一个2d数组,我需要找到所有行的和,输出应该打印最高和值的索引 我的代码给出了第2行的总和23,但我想要的是答案2(索引),而不是23(总和) 公共静态int indexofHighestRowSum(int[]]a) { int i,j,和,n=0; 对于(i=0;i您需要跟踪当前最高总和旁边的索引。然后您可以返回遇到的最高行的索引: public static int indexofHighestRowSum (int[][] a) { int i,j, sum,

这是一个2d数组,我需要找到所有行的和,输出应该打印最高和值的索引

我的代码给出了第2行的总和23,但我想要的是答案2(索引),而不是23(总和)

公共静态int indexofHighestRowSum(int[]]a)
{
int i,j,和,n=0;

对于(i=0;i您需要跟踪当前最高总和旁边的索引。然后您可以返回遇到的最高行的索引:

public static int indexofHighestRowSum (int[][] a)
   {
        int i,j, sum, n=0;
        for (i=0; i<a.length; i++)
        {
            sum =0;
            for (j=0; j<a[0].length; j++)
             {
                  sum += a[i][j];
             }

             if (sum >n)
               n=sum;`
        }
        return a[index];
    }
公共静态int indexofHighestRowSum(int[]]a){
int i,j,sum,n=0,index=0;
对于(i=0;in){
n=总和;
指数=i;
}
}
收益指数;
}

请查看该网站的工作原理、主题中的问题以及相应的问题。另请参见:将
i
保存在n中,而不是
sum
,然后返回n;回答很好!但我使用了index=0,当sum>n时,我确实使用了index=i和return索引。
public static int indexofHighestRowSum (int[][] a)
   {
        int i,j, sum, n=0;
        for (i=0; i<a.length; i++)
        {
            sum =0;
            for (j=0; j<a[0].length; j++)
             {
                  sum += a[i][j];
             }

             if (sum >n)
               n=sum;`
        }
        return a[index];
    }
public static int indexofHighestRowSum(int[][] a) {
    int i, j, sum, n = 0, index = 0;
    for (i = 0; i < a.length; i++) {
        sum = 0;
        for (j = 0; j < a[0].length; j++) {
            sum += a[i][j];
        }

        if (sum > n) {
            n = sum;
            index = i;
        }
    }

    return index;
}