静态方法和Jagged Arrays

时间:2015-01-16 05:30:27

标签: java arrays jagged-arrays

我正在尝试编写一个返回整数的静态方法,将一个二维整数数组作为参数,并返回具有最大总和的二维数组(锯齿状数组)中的行的索引所有元素。沿着这条路出了点问题,我还是想弄明白。请帮忙吗?
这是代码:

 public static int findMaxRow(int[][] maxRows){
        newI= 0;
        newJ= 0;
        for(int i=0; i< maxRows.length; i++) {
            newI += i;
            for(int j=0; j< maxRows.length; j++) {
                newJ += j;
           ``  if( newI > newJ){
               return newI;
             else {
             }
           }
         }
     }

2 个答案:

答案 0 :(得分:1)

您永远不会定义newInewJ的类型,可以通过在声明之前使用其预期类型(即int)来修复。你还有两个&#34; `&#34;在你的if语句之前,你错过了一个结束括号&#34; }&#34;在你的声明之前。但那些只是语法错误。一旦你修复了这些错误,你就会注意到你的方法没有返回所需的结果。

查看代码,特别是for循环。

for(int i=0; i< maxRows.length; i++) {
    newI += i;
    for(int j=0; j< maxRows.length; j++) {
        newJ += j;
        // other stuff
    }
 }

让我们说maxRows.length等于3.这意味着外循环将从0运行到2,因此newI将等于3.同时对于每次迭代,外循环使,内循环迭代3次。所以newJ最终将等于9.这不是对数组元素求和的正确方法。更好的方法是迭代外部循环中的数组并对内部循环中的元素求和,然后进行比较以完成外部循环。像这样:

int largestRow = 0;
int largestSum = 0;
int sum;

// iterate over each array
for(int i=0; i< maxRows.length; i++) {
    sum = 0; // set and reset sum to zero

    // iterate over each element
    for(int j=0; j< maxRows[i].length; j++) {
        sum += maxRows[i][j];
    }

    // if sum is > the previous largest sum then set largest
    // sum to this new sum and record which row
    if(sum > largestSum) {
        largestRow = i;
        largestSum = sum;
    }
}

return largestRow;

以下是您尝试完成的示例。

public class RowSums {

    public static void main(String[] args) {
        int[][] test = { {1, 5, 7, 0, 9} , {2, 4, 5, 6, 7} , {9, 2, 0, 12, 8, 3} };

        System.out.println(printRows(test));
        System.out.println("The row with the largest sum is row "
                           + findMaxRow(test));
    }

    public static int findMaxRow(int[][] maxRows){
        int largestRow = 0;
        int largestSum = 0;
        int sum;

        // iterate over each array
        for(int i=0; i< maxRows.length; i++) {
            sum = 0; // set and reset sum to zero

            // iterate over each element
            for(int j=0; j< maxRows[i].length; j++) {
                sum += maxRows[i][j];
            }

            // if sum is > the previous largest sum then set largest
            // sum to this new sum and record which row
            if(sum > largestSum) {
                largestRow = i;
                largestSum = sum;
            }
        }

        return largestRow;
     }

    public static String printRows(int[][] rows) {
        StringBuilder s = new StringBuilder("Rows and their sums:\n");
        int sum;

        for(int x = 0; x < rows.length; x++) {
            s.append("Row [" + x + "] = [ ");
            sum = 0;
            for(int y = 0; y < rows[x].length; y++) {
                s.append(rows[x][y] + " ");
                sum += rows[x][y];
            }
            s.append("]\n");
            s.append("Row [" + x + "]'s sum is " + sum + "\n");
        }

        return s.toString();
    }

}

输出:

Rows and their sums:
Row [0] = [ 1 5 7 0 9 ]
Row [0]'s sum is 22
Row [1] = [ 2 4 5 6 7 ]
Row [1]'s sum is 24
Row [2] = [ 9 2 0 12 8 3 ]
Row [2]'s sum is 34

The row with the largest sum is row 2

答案 1 :(得分:0)

修改程序,以下示例将返回其中元素总和最大的行的索引。

让我们假设要传递的数组是:

int [][] maxRows = {{1,2,3}, {1,2,3,4,5}, {9,9,9,9}, {1,2}};

在方法

中传递此数组
     public static int findMaxRow(int[][] maxRows){
        int sum = Integer.MIN_VALUE;

       int biggestIndex= 0;
       for(int i = 0; i<maxRows.length; i++){
           int temp = 0;
           for(int ir : maxRows[i]){
            temp+=ir;
           }
          if(temp>sum){
             sum = temp;
             biggestIndex = i;
         }
      }

            return biggestIndex;

     }

上面的程序将返回具有最大元素总和的内部数组的索引,在上面的例子中,它将返回2