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(); } }}上面的代碼來(lái)自https://rosettacode.org/wiki/Averages/Simple_moving_average#Java當(dāng)我創(chuàng)建一個(gè)名為 SimpleMovingAverage 的類(lèi)并從上述網(wǎng)站復(fù)制代碼時(shí),會(huì)報(bào)告錯(cuò)誤?!癹ava.awt.Queue”在“java.awt”中不是公開(kāi)的。無(wú)法從外部包訪問(wèn)怎么解決呢?
1 回答

DIEA
TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超3個(gè)贊
不需要java.util.Queue
,java.awt.Queue
里面可以裝任何你想要的東西
該java.awt
包是關(guān)于 UI、圖形和圖像的:Documentation
,此處java.awt.Queue
用于保存java.awt.Event
元素
僅用于改進(jìn):對(duì)于循環(huán) FIFO 的實(shí)現(xiàn),這里有一些信息
是否有一個(gè)固定大小的隊(duì)列可以刪除過(guò)多的元素?
CircularQueue的基本實(shí)現(xiàn)
這可以給類(lèi)似的東西
public?void?newNum(double?num)?{ ????window.add(num); }
添加回答
舉報(bào)
0/150
提交
取消