JAVA: calculate the average between two or more arrays [closed]

This code will solve your problem:

public static double[] averages(double[]... input) {

    double[] result = new double[input.length];

    for(int i = 0; i< result.length; i++) {
        result[i] = Arrays.stream(input[i]).average().getAsDouble();
    }

    return result;
}

The code uses the VarArgs, so allows you to call it with various arguments.

double[] avgs = averages(a1,a2);

will result with array that has two doubles, each being an avg of items in array.

It uses Java 8 piece but it can be replaced by the code that calculate avg from single array.


In case of your code you have an issues.

Java do not know instruction like this

while(int i=0; i < data.length; i++)

it should be replaced with for

for(int i=0; i < data.length; i++)

Leave a Comment