java版本:SDK 1.8.0_151
IDE:IDEA IntelliJ
import java.awt.Queue;
import java.util.LinkedList;
public class SimpleMovingAverage {
private final Queue<Double> window = new LinkedList<Double>();
private final int period;
private double sum;
public SimpleMovingAverage(int period) {
assert period > 0 : "Period must be a positive integer";
this.period = period;
}
public void newNum(double num) {
sum += num;
window.add(num);
if (window.size() > period) {
sum -= window.remove();
}
}
public double getAvg() {
if (window.isEmpty()) {return 0.0;} // technically the average is undefined
return sum / window.size();
}
public static void main(String[] args) {
double[] testData = {1, 2, 3, 4, 5, 5, 4, 3, 2, 1};
int[] windowSizes = {3, 5};
for (int windSize : windowSizes) {
SimpleMovingAverage ma = new SimpleMovingAverage(windSize);
for (double x : testData) {
ma.newNum(x);
System.out.println("Next number = " + x + ", SMA = " + ma.getAvg());
}
System.out.println();
}
}
}
上面的代码来自https://rosettacode.org/wiki/Averages/Simple_moving_average#Java
当我创建一个名为SimpleMovingAverage的类并从上述网站复制代码时,会报告错误。
'java.awt.Queue'在'java.awt'中不公开。无法从外部软件包访问
如何解决?
答案 0 :(得分:1)
您需要java.util.Queue
而不是java.awt.Queue
,它可以容纳您想要的任何东西
java.awt
软件包涉及UI,图形和图像:Documentation
,而java.awt.Queue
在这里用于容纳java.awt.Event
元素
仅用于改进:有关实施循环FIFO的信息,在此处
哪个可以给类似的东西
public void newNum(double num) {
window.add(num);
}