简体   繁体   English

Java中的类型兼容性问题

[英]Type compatibility issues in Java

I am trying to implement Kruskal's algorithm for a minimum spanning tree in java. 我正在尝试为Java中的最小生成树实现Kruskal算法。 I am struggling with type incompatibilities. 我正在为类型不兼容而苦苦挣扎。 Throughout the program I have several for loops that look like this: 在整个程序中,我有几个for循环,如下所示:

for (Edge f : G.edges()) {
            int x = f.either(), y = f.other(x);
            if (!uf.connected(x, y)) {
                if (f.weight() < e.weight()) {
                    System.err.println("Edge " + f + " violates cut optimality conditions");
                    return false;
                }
            }
        }

All of the for loops I have look similar to this with the for either having 我看到的所有for循环都与此类似,

Edge f : G.edges()

or 要么

Edge e : G.edges()

inside the parentheses. 在括号内。 In this case, Edge is another class within the project Edge.java, and edges is a method within this class: 在这种情况下,Edge是项目Edge.java中的另一个类,而edge是此类中的方法:

for (Edge e : edges()) {

        // all edges in MST except e
        uf = new UF(G.V());
        for (Edge f : mst) {
            int x = f.either(), y = f.other(x);
            if (f != e) uf.union(x, y);
        }

When this code is put into NetBeans, the lines containing the for loop produce an type error, "incompatible types: com.sun.javafx.geom.Edge cannot be converted to kruskal.Edge" NetBeans recommends fixing this by changing the types of my f and e variables to edges. 当将此代码放入NetBeans时,包含for循环的行会产生类型错误,“不兼容的类型:com.sun.javafx.geom.Edge无法转换为kruskal.Edge” NetBeans建议通过更改我的类型来解决此问题。将f和e变量移至边缘。 Doing this would make my code look like this: 这样做会使我的代码看起来像这样:

for (com.sun.javafx.geom.Edge e : G.edges()) {
        int v = e.either(), w = e.other(v);
        if (!uf.connected(v, w)) {
            System.err.println("Not a spanning forest");
            return false;
        }
    }

The only problem with this solution is, when I implement it, it fails to recognize my either() and other() methods that are implemented in my Edge.java file. 此解决方案的唯一问题是,当我实现它时,它无法识别在Edge.java文件中实现的我的any()和other()方法。 Here is a sample of my other() method: 这是我的other()方法的示例:

public int other(int vertex) {
    if      (vertex == v) return w;
    else if (vertex == w) return v;
    else throw new IllegalArgumentException("Illegal endpoint");
}

I believe there is a simple solution to this problem that I am unable to think of. 我相信有一个我无法想到的简单解决方案。 What would be the best way to go about fixing my type issue while still being able to access my methods from the Edge class? 解决我的类型问题同时仍然能够从Edge类访问我的方法的最佳方法是什么?

Remove all occurences of "com.sun.javafx.geom.Edge" 删除所有出现的“ com.sun.javafx.geom.Edge”

And in the import statement use only the kruskal.Edge 并且在import语句中仅使用kruskal.Edge

The problem is that you are referencing the wrong Edge class. 问题是您引用了错误的Edge类。

for (com.sun.javafx.geom.Edge e : G.edges()) {
    int v = e.either(), w = e.other(v);
    if (!uf.connected(v, w)) {
        System.err.println("Not a spanning forest");
        return false;
    }
}

should be something like: 应该是这样的:

for (com.your.project.model.Edge e : G.edges()) {
    int v = e.either(), w = e.other(v);
    if (!uf.connected(v, w)) {
        System.err.println("Not a spanning forest");
        return false;
    }
}

or just import the class properly. 或仅正确导入该类。 Check your current imports and make sure you aren't importing Sun's Edge explicitly. 检查当前的导入,并确保未明确导入Sun's Edge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM