试图在2d数组中找到EACH行的平均值

时间:2015-09-15 02:52:15

标签: java

我在这里尝试做的是得到我的2D数组中每行的平均值。它只输出总计-780-为rawTotal当我希望第一行为100并且yada yada ......有人可以指导我从哪里开始正确的方向吗?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at ArrayHW.averageRow(ArrayHW.java:40)
    at ArrayHW.main(ArrayHW.java:10)

public class ArrayHW {
    public static void main(String[] args){
        int [][] a2 = {{10,20,30,40},
                {50, 60, 70 , 80},
                {90, 100, 110, 120}};
        display2DArray(a2);
        System.out.println(average2DArray(a2));
        System.out.println(min2DArray(a2));
        System.out.println(averageRow(a2));
    }

    public static int display2DArray(int[][] a2) {
        for (int row = 0; row < 3; row++){
            for (int column = 0; column < 4; column++){
                System.out.println(a2[row][column]);
            }
        }
        return 0;
    }

    public static double average2DArray(int[][] a2) {
        double sum = 0.0;
        int count = 0;

        for (int row = 0; row < a2.length; row++) {
            for (int column = 0; column < a2[row].length; column++) {
                sum += a2[row][column];
                count++;
            }
        }
        return sum / count;
    }

    public static double averageRow(int[][] a2) {
        int rowTotal = 0;
        double average = 0;

        for (int row = 0; row < a2.length; row++){
            for (int column = 0; column < a2[row].length; column++){
                rowTotal += a2[row][column];

                //average = rowTotal / a2[row].length;
            }
        }
        return rowTotal;

    }

2 个答案:

答案 0 :(得分:0)

对其中两种方法进行了一些修正。有关进一步说明,请参阅代码注释:

public static int display2DArray(int[][] a2) {
    for (int row = 0; row < 3; row++){
        for (int column = 0; column < 4; column++){
            System.out.print(a2[row][column] + " "); // better display
        }
        System.out.println(); // better display
    }
    return 0;
}

public static double averageRow(int[][] a2) {
    int rowTotal = 0;
    double average = 0;

    for (int row = 0; row < a2.length; row++){
        for (int column = 0; column < a2[row].length; column++){
            rowTotal += a2[row][column];
        }
        average = rowTotal / a2[row].length; // calc average
        System.out.println(average); // print the row average
        rowTotal = 0; // start over (for next row)
    }
    return rowTotal;
}

averageRow的输出:

25.0
65.0
105.0

答案 1 :(得分:0)

因为你有多行而你想在每一行上返回一些计算,你需要返回一个值数组

这意味着您需要创建一个数组,其中可以存储每行计算的结果。您还需要确保每行都使用一组新值来表示,例如......

public static double[] averageRow(int[][] a2) {

    double average[] = new double[a2.length];
    for (int row = 0; row < a2.length; row++) {
        int rowTotal = 0;
        for (int column = 0; column < a2[row].length; column++) {
            rowTotal += a2[row][column];
        }

        average[row] = rowTotal / a2[row].length;
    }
    return average;

}

然后使用类似的东西

int[][] a2 = {{10, 20, 30, 40},
{50, 60, 70, 80},
{90, 100, 110, 120}};
System.out.println(Arrays.toString(averageRow(a2)));

将输出[25.0, 65.0, 105.0]