简体   繁体   English

如何使用do循环在数组列表中找到双精度值的平均值?

[英]How to find the average of double values in an array list using a do loop?

I am creating a method called getAveragePrice and I need it to calculate the average of values in an undefined array list (which would only be defined during the testing/implementation of said method).我正在创建一个名为 getAveragePrice 的方法,我需要它来计算未定义数组列表中值的平均值(仅在所述方法的测试/实现期间定义)。

    public double getAveragePrice() {
    double sum = 0;
    int x = 0; //counting variable
    do {

That is what I have right now for a start.这就是我现在所拥有的。 I know that every time a value is added to the sum the count of x needs to increase by 1 so the method knows what to divide the final sum by.我知道每次将一个值添加到总和时,x 的计数需要增加 1,因此该方法知道将最终总和除以什么。 The only issue I have is how to set up a do loop to give me the sum of the values in an array list.我唯一的问题是如何设置一个 do 循环来给我一个数组列表中的值的总和。

You can find the average in one line using Java 8 streams:您可以使用 Java 8 流在一行中找到平均值:

List<Double> vals;
// initialize vals
double avg = vals.stream().mapToDouble(Double::doubleValue).sum() / vals.size();

You can also iterate here, which is slighly more work:你也可以在这里迭代,这是稍微多一些的工作:

double sum = 0.0;
for (double val : vals) {
    sum += val;
}

double avg = vals.size() > 0 ? sum / vals.size() : 0.0d;

You should loop through each element in the list.您应该遍历列表中的每个元素。

public double getAveragePrice() {
double sum = 0;
int x = 0; //counting variable
    do {
        sum += list.get(x);
        x++;
    } while (x < list.size());
int average = sum / x;
return average;
}

Basically, you start off the loop as x = 0, and each loop you add the value of that index, then increase x by 1. You then check if x is less than the total number of elements.基本上,当 x = 0 时开始循环,每次循环添加该索引的值,然后将 x 增加 1。然后检查 x 是否小于元素总数。 If it is, then there are more elements in the list to add.如果是,则列表中还有更多要添加的元素。 If not, then you already summed all the elements.如果没有,那么您已经汇总了所有元素。 Since you added all the elements, you just need to divide it by x and then return that as the average.由于您添加了所有元素,因此只需将其除以 x,然后将其作为平均值返回。

You can have do.while condition as:您可以将 do.while 条件设为:

do{
        Sum += arr[x];
        x++;
 }While(x<arr.length)

And then for avg:然后对于平均:

Avg = sum/x

Alternatively you could use for(each) instead of do/while:或者,您可以使用 for(each) 而不是 do/while:

List<Double> values; // List<double> is no good apparently
double average = 0;
for (Double value : values) {
    average += value.doubleValue(); // I miss PHP ;)
}
average /= values.size();

This is a stylistic thing mostly, but I like to avoid explicitly using array indices to access collections (lists, arrays, whatever the language calls them) when you don't care about the order of your elements and don't intend to alter any of them.这主要是一种风格,但是当您不关心元素的顺序并且不打算更改任何元素时,我喜欢避免显式使用数组索引来访问集合(列表、数组,无论语言如何称呼它们)其中。

This means you don't have a counter/pointer variable hanging around that you don't actually need later (you only care about its value during the loop) and you can't accidentally mess with your original data (the '==' to '=' typo is a classic).这意味着你没有一个计数器/指针变量,你以后实际上不需要(你只关心它在循环过程中的值)并且你不会不小心弄乱你的原始数据('==' '=' 错字是经典之作)。

Caution: I don't write much Java so perhaps foreach is frowned upon?注意:我写的 Java 不多,所以 foreach 可能不受欢迎?

Edit: I don't write enough Java to use its collections correctly.编辑:我没有编写足够的 Java 来正确使用它的集合。

A Java 8 loopless solution: Java 8 无环解决方案:

myDoubleList.stream()
  .mapToDouble(Double::doubleValue)
      .average()
      .orElse(Double.NaN);

It's 21st century.现在是 21 世纪。 No need to use any loop.无需使用任何循环。 Use Scala & just write使用 Scala & 只写

def exercise5(a: Array[Double]): Double = {
    a.sum / a.length
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM