我正在向 PriorityQueue 添加边,但由于某种原因,它们没有按它们的值进行排序,导致以后出现错误结果。
我的边缘类看起来像这样
class Edge implements Comparable<Edge>{
int value;
String dest;
String start;
public Edge(String start, String dest, int g) {
this.dest = dest;
value = g;
this.start = start;
}
@Override
public int compareTo(Edge o) {
int temp = value - o.value;
if (temp > 0) {
return 1;
}
if (temp < 0) {
return -1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然而,当我运行我的代码,在属于节点“Springfield,MO”的 LinkedList 上执行 addAll 到 PriorityQueue 时,边缘按错误的顺序排序,如下所示,有什么问题?
queue.addAll(list.get(node));
Run Code Online (Sandbox Code Playgroud)
我试图为 Edge 制作一个特定的比较器类并将其用作 PriorityQueue 中的参数,但我仍然得到相同的结果。
当我在开发环境(Eclipse)中启动应用程序时,它会运行,但是,当我尝试将其导出到可运行的 .jar 文件时,它会出现以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at mallApp.MainTwo.<clinit>(MainTwo.java:24)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:398)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:410)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:405)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:695)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:657)
at javafx.scene.control.Control.<clinit>(Control.java:99)
... 4 more
Run Code Online (Sandbox Code Playgroud)
问题是“由以下原因引起:java.lang.IllegalStateException:工具包未初始化”。
这里有很多关于类似问题的线程,但它们似乎都不完全相同,而且我尝试过的修复对我不起作用。我的类扩展了 Application,因此在 main 方法中具有 Application.launch(args) 。这应该初始化 Toolkit,但在导出到 .jar 时由于某种原因却没有初始化。
当我尝试以不同的方式添加 Toolkit 时,例如使用 JFXPanel 或Platform.startup(Runnable),它会给出相反的错误,提示 Toolkit 已初始化。
public class MainTwo extends Application {
...
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage arg0) throws Exception {
model = …Run Code Online (Sandbox Code Playgroud)