gpt4 book ai didi

java - 无法从外部包访问java.awt.Queue

转载 作者:行者123 更新时间:2023-12-02 11:12:25 26 4
gpt4 key购买 nike

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' is not public in 'java.awt'. Cannot be accessed from outside package



如何解决?

最佳答案

您需要java.util.Queue而不是java.awt.Queue,它可以容纳您想要的任何内容
java.awt包是关于UI,图形和图像的: Documentation ,并且java.awt.Queue在这里用于保存java.awt.Event元素

仅用于改进:用于实现循环FIFO,此处有一些信息

  • Is there a fixed sized queue which removes excessive elements?
  • Basic implem of a CircularQueue

  • 这可能会给像
    public void newNum(double num) {
    window.add(num);
    }

    关于java - 无法从外部包访问java.awt.Queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58604291/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com