天天看点

java中average方法_Java LongSummaryStatistics getAverage()用法及代码示例

Java中LongSummaryStatistics类的getAverage()方法用于获取此LongSummaryStatistics中记录的平均值。

用法:

public double getAverage()

参数:此方法不接受任何值作为参数。

返回值:此方法返回此LongSummaryStatistics中记录的平均值。

程序:

// Java program to demonstrate

// the above method

import java.util.*;

public class LongSummaryStatisticsDemo {

public static void main(String[] args)

{

LongSummaryStatistics longSummaryStatistics

= new LongSummaryStatistics();

List list

= Arrays.asList(10, 20, 30, 40, 50);

Iterator iterator = list.listIterator();

while (iterator.hasNext()) {

// Add the integers to the LongSummaryStatistics object

longSummaryStatistics.accept(iterator.next());

}

System.out.println("The average of values is "

+ longSummaryStatistics.getAverage());

}

}

输出:

The average of values is 30.0