English 中文(简体)
在可变的双倍数中确定平均数
原标题:Find the average within variable number of doubles

I have an ArrayList of doubles, and i need to find the average between all numbers. The amount of Double instances in the arraylist is not constant, could be 2, could be 90 I have been trying for hours to get the algorithm myself but could not get it to work in anyway.

你有什么建议? 或者,你能否把我与现有的图书馆联系起来,以获得这一平均数?

谢谢。

问题回答

创建<条码>。 变量:

double sum = 0;

采用排行方式通过清单中的内容:

for (double d : yourList)

在每一版本中添加<代码>sum。 现行价值:

    sum += d;

为找到平均数,将<代码>sum与清单中要素数分开:

double avg = sum / yourList.size();

这里的指认为过于简单的

上述解决办法实际上并不完美。 如果名单上的头几个要素极为庞大,最后几个要素是小的,那么由于两倍的精确性,<代码>sum += d可能不会对最终结果产生任何影响。

解决办法是在进行平均计算之前对清单进行分类:

Collections.sort(doublesList);

Now the small elements will be added first, which makes sure they are all counted as accurately as possible and contribute to the final sum :-)

如果你们喜欢流子的话:

List<Number> list = Arrays.asList(number1, number2, number3, number4);

// calculate the average
double average = list.stream()
                     .mapToDouble(Number::doubleValue)
                     .average()
                     .getAsDouble();

平均定义是所有价值的总和,按价值数目分列。 超越价值观,增加这些价值观,按清单的规模划分。

If by "average between all numbers" you mean the average of all the doubles in your list, you can simply add all the doubles in your list (with a loop) and divide that total by the number of doubles in your list.

您:

List<Double> list = new ArrayList<Double>();
...    
double countTotal = 0;

for (Double number : list)
{
    countTotal += number;
}

double average = countTotal / list.size();

也许我要提一下问题......但这不是什么样?

double sum = 0.0;
for (double element : list) {
    sum += element;
}
double average = sum / list.size();

如果你把整个名单一概归一旁,就重新担心过多,你可以使用一个正常的平均数。 由于它拥有更多的业务,将比增加所有活动并一劳永逸。

for (int x = 0; x < list.size(); x++)
   average = (average / (x+1)) * (x) + list.get(x) / (x+1);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签