Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java中的数组计算数据表中行的平均值_Java_Arrays_Methods - Fatal编程技术网

使用Java中的数组计算数据表中行的平均值

使用Java中的数组计算数据表中行的平均值,java,arrays,methods,Java,Arrays,Methods,我试图计算行和列的平均值。到目前为止,我可以得到列的平均值,但是,我很难计算行的平均值 我的数据表是这样的 Example1 Example2 Example2 85 75 92 80 91 89 85 52 78 到目前为止,我所做的是将每列作为一个数组,如下所示: int[] Example1 = {85, 80, 85}; int[] E

我试图计算行和列的平均值。到目前为止,我可以得到列的平均值,但是,我很难计算行的平均值

我的数据表是这样的

Example1      Example2    Example2  
 85              75         92
 80              91         89
 85              52         78
到目前为止,我所做的是将每列作为一个数组,如下所示:

int[] Example1 = {85, 80, 85};
int[] Example2 = {75, 91, 52};
int[] Example3 = {92, 89, 78};
然后我创建了一个这样的方法来计算表中每列的平均值

public static void avg_calc(int[] examples) { 
    int sum = 0;
    int avg;
    for (int i = 0; i < examples.length; i ++) {
        sum += examples[i];
    }
    avg = sum/examples.length;
    System.out.println("Average is " + avg);
}   
这样,当我进行avg_CalceExample1时,我可以计算'Example1'数组的平均值,即83

但是,我想计算示例1、示例2和示例3的平均值,例如,第一行的平均值为84

如何在函数中添加另一个数组来计算行的平均值

任何帮助都将不胜感激

如何在函数中添加另一个数组来计算平均值 划船

一种方法是:

    int[] Example1 = {85, 80, 85};
    int[] Example2 = {75, 91, 52};
    int[] Example3 = {92, 89, 78};
    int[][] examples = {Example1, Example2, Example3}; // new array

    for (int i = 0; i < examples[0].length; i++) {
        double rowAverage = 0;
        for (int[] arr : examples) {
                rowAverage += arr[i];
        }
        System.out.println("Average of row " + (i + 1) + ": " + rowAverage / examples.length);
    }        
如何在函数中添加另一个数组来计算平均值 划船

一种方法是:

    int[] Example1 = {85, 80, 85};
    int[] Example2 = {75, 91, 52};
    int[] Example3 = {92, 89, 78};
    int[][] examples = {Example1, Example2, Example3}; // new array

    for (int i = 0; i < examples[0].length; i++) {
        double rowAverage = 0;
        for (int[] arr : examples) {
                rowAverage += arr[i];
        }
        System.out.println("Average of row " + (i + 1) + ": " + rowAverage / examples.length);
    }        

@alfasin的答案很好,下面是一个java 8解决方案:

public static int getRowAverage(int index, int[]... examples) {
    return Arrays.stream(examples)
            .mapToInt(ex -> ex[index]).sum() / examples.length;

}
您可以尝试以下方法:

public static void main(String[] args) {
    int[] example1 = {85, 80, 85};
    int[] example2 = {75, 91, 52};
    int[] example3 = {92, 89, 78};
    // for row 1 (index 0)
    int average_row_1 = getRowAverage(0, example1, example2, example3);
    System.out.println(average_row_1);
}

@alfasin的答案很好,下面是一个java 8解决方案:

public static int getRowAverage(int index, int[]... examples) {
    return Arrays.stream(examples)
            .mapToInt(ex -> ex[index]).sum() / examples.length;

}
您可以尝试以下方法:

public static void main(String[] args) {
    int[] example1 = {85, 80, 85};
    int[] example2 = {75, 91, 52};
    int[] example3 = {92, 89, 78};
    // for row 1 (index 0)
    int average_row_1 = getRowAverage(0, example1, example2, example3);
    System.out.println(average_row_1);
}

通过利用mapToInt中的lambda,您可以使其更短:Arrays.streamexamples.MapToIntar->arr[index].sum/examples.length@PaulLemarchand谢谢,这似乎是一个很好的答案,作为Java新手,这对我来说似乎有点高级。您可以通过利用mapToInt中的lambda来缩短它:Arrays.streamexamples.MapToIntar->arr[index]。sum/examples.length@PaulLemarchand谢谢你,这似乎是一个很好的答案,作为Java新手,这对我来说似乎有点高级。太好了,我在我的方法中添加了int[][]示例,并像你一样创建了迭代示例。当我通过avg_calcExample1时,我得到了每列平均值下重复的行平均值。我想我必须修改我的打印语句。实际上我更喜欢@Paul Lemarchand的回答:这样你就可以编写一个方法来计算行索引的平均值,并根据需要的次数来调用它。它的模块化程度更高。很好,我在我的方法中添加了int[][]示例,并创建了迭代示例。当我通过avg_calcExample1时,我得到了每列平均值下重复的行平均值。我想我必须修改我的打印语句。实际上我更喜欢@Paul Lemarchand的回答:这样你就可以编写一个方法来计算行索引的平均值,并根据需要的次数来调用它。它最终变得更加模块化。