I am not sure what you are asking in the question, but I think this is what you are looking for.
public static double calculateAll(List<Double> allNumbers) {
double average;
double total = 0.0;
for (Double allNumber : allNumbers) {
total += allNumber;
}
average = total / allNumbers.size();
return average;
}
You were storing an Array Inside a Collection. So I have changed that to Double
note that double
is native while Double
isn't. You can't have double
inside a Collection. and then I have converted the for
loop into a foreach
loop
Here is how you can call this code.
public static void main(String[] args) {
List<Double> doubles = new ArrayList<>();
doubles.add(0.1);
doubles.add(4.1);
double wat = calculateAll(doubles);
System.out.println(wat);
}
So lets take a note of all the changes we have done.
List<double[]>
has been replaced with List<Double>
.
List
now holds items of instance Double
for(int i = 0; i < allNumbers.size(); i++)
was changed to for (Double allNumber : allNumbers)
this is just a basic for-each loop.
Honestly I would just use java 8 for this, We can do this in 1 line!
public static double calculateAll(List<Double> allNumbers) {
return allNumbers.stream().mapToDouble(e -> e / allNumbers.size()).sum();
}