Sum all the elements java arraylist
Two ways:
Use indexes:
double sum = 0;
for(int i = 0; i < m.size(); i++)
sum += m.get(i);
return sum;
Use the "for each" style:
double sum = 0;
for(Double d : m)
sum += d;
return sum;
Using Java 8 streams:
double sum = m.stream()
.mapToDouble(a -> a)
.sum();
System.out.println(sum);