我正在研究Java应用程序。有一个与整数字段(分数)相对应的Getter。我的目标是计算该字段的平均值。我决定制作一个数组,然后计算该数组的数量和总和。但是我真的陷入了Java语法和“心理状态”的困扰。
这是我的摘录:
public void setPersonData2(List<Person> persons2) { // Try to make a count of the array int[] scoreCounter = new int[100]; // 100 is by default since we don't know the number of values for (Person p : persons2) { int score = p.getScoreTheo(); // Getter Arrays.fill(scoreCounter, score); // Try to delete all values equal to zero int[] scoreCounter2 = IntStream.of(scoreCounter).filter(i -> i != 0).toArray(); // Calculate count int test = scoreCounter2.length; System.out.println(test); } }
您能帮我吗?
我正在研究Java应用程序。有一个与整数字段(分数)相对应的Getter。我的目标是计算该字段的平均值。我决定制作一个数组,然后计算计数...
为什么计算简单平均值太复杂?而且,我不明白为什么要使用数组。
使用流:
Stream API具有内置的求平均值功能。