java版本:SDK 1.8.0_151IDE:IntelliJ IDEAimport 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”中不是公开的。无法从外部包访问怎么解决呢?
1 回答
DIEA
TA贡献1820条经验 获得超2个赞
不需要java.util.Queue
,java.awt.Queue
里面可以装任何你想要的东西
该java.awt
包是关于 UI、图形和图像的:Documentation
,此处java.awt.Queue
用于保存java.awt.Event
元素
仅用于改进:对于循环 FIFO 的实现,这里有一些信息
是否有一个固定大小的队列可以删除过多的元素?
CircularQueue的基本实现
这可以给类似的东西
public void newNum(double num) { window.add(num); }
添加回答
举报
0/150
提交
取消