I'm trying to make an average (moy) of an arrayList's column but I need to sum it first, I don't know how to do it
int sumX = 0;
val = new ArrayList<>();
float moy[] = new float[3];
if (i < 100) {
val.add(sensorEvent.values);
i++;
} else {
for (; i > 0; i--);
{
sumX = ?
moy[0] = sumX/100;
}
val.clear();
i = 0;
}
You can use this code snippet: (Written in kotlin)
fun main() {
val s: List<Float> = listOf(1.2F, 1.4F, 5.6F)
// populate it with your custom list data
var sum = 0F
s.forEach { it ->
sum+=it
}
println("Sum = $sum and avg = ${sum/(s.size)}")
}
Well here is the java solution:
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
List<Float> s = new ArrayList(Arrays.asList(1.2F, 1.3F, 5.4F, 6.5F));
Float sum = 0F;
for (int i=0; i< s.size(); i++) {
sum+=s.get(i);
}
System.out.println("Sum: "+sum+" and Avg: "+sum/(s.size()));
}
}