嘿我现在在我的程序的最后一点,我遇到了最奇怪的错误,基本上我正在比较两个包含两个字符串的对象,所以我将字符串的所有组合进行比较,看看是否对象由一个共同的字符串相关联。
这是一个kruskals循环检查器,所以它比这更复杂,但这就是这个错误的基础,我添加了一些手动system.out调试,它告诉我最奇怪的事情,例如这是它的基础,它只将label1与label1进行比较,但重复进行 1 1 1 2 2 1 2 2
private static ArrayList<Graph> caseForConnection(
Connection consideredConnection, ArrayList<Graph> subTrees)
throws Exception {
ArrayList<Graph> ret = new ArrayList<Graph>();
System.out.println("Considering connection " +consideredConnection.getSaveDetails()[1] +consideredConnection.getSaveDetails()[2] + " "+consideredConnection.getSaveDetails()[3]);
String label1 = consideredConnection.getNode(1).getLabel();
String label2 = consideredConnection.getNode(2).getLabel();
for (Graph x : subTrees) {
for (Connection c : x.getConnectionList()) {
System.out.println("Comparing to connection "+ c.getSaveDetails()[1] +c.getSaveDetails()[2] +" " +c.getSaveDetails()[3]);
if (c.getNode(1)
.getLabel()
.equalsIgnoreCase(label1)) {
if (ret.contains(x)) {
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label1+ " And this is twice"));
throw new Exception("Cycle Found");
}
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label1));
ret.add(x);
} else if (c
.getNode(1)
.getLabel()
.equalsIgnoreCase(label2)) {
if (ret.contains(x)) {
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label1+ " And this is twice"));
throw new Exception("Cycle Found");
}
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label2));
ret.add(x);
}
if (c.getNode(2)
.getLabel()
.equalsIgnoreCase(label1)) {
if (ret.contains(x)) {
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label1+ " And this is twice"));
throw new Exception("Cycle Found");
}
System.out.print(("Found " + c.getNode(2)
.getLabel() +" is the same as " + label1));
ret.add(x);
} else if (c
.getNode(2)
.getLabel()
.equalsIgnoreCase(label2)) {
if (ret.contains(x)) {
System.out.print(("Found " + c.getNode(1)
.getLabel() +" is the same as " + label1+ " And this is twice"));
throw new Exception("Cycle Found");
}
System.out.print(("Found " + c.getNode(2)
.getLabel() +" is the same as " + label2));
ret.add(x);
}
}
}
return ret;
}
现在奇怪的是,我的控制台输出了这个
发现C与B相同 发现D与A
相同通过连接 AC 公元前 BD 广告 CG BF GF CF 其中label1将是a,并且第一个标记为2 c,它只发生在这两个上,当然,这会给我一个无效的结果。
答案 0 :(得分:0)
您的代码非常重复,这使得它暴露在如此疯狂的错误中。你为什么不加入followinrg:
在节点中:
String normLabel() { return getLabel().toUpperCase(); }
在连接中:
public boolean matches(Connection other) {
final String this1 = getNode(1).normLabel(), this2 = getNode(2).normLabel(),
other1 = other.getNode(1).normLabel(),
other2 = other.getNode(2).normLabel();
return this1.equals(other1) || this2.equals(other1) ||
this1.equals(other2) || this2.equals(other2);
}
然后您的主代码可以折叠成以下内容:
private static List<Graph> caseForConnection(
Connection consideredConnection, ArrayList<Graph> subTrees)
{
final List<Graph> ret = new ArrayList<Graph>();
for (Graph g : subTrees) {
for (Connection c : g.getConnectionList()) {
if (c.matches(consideredConnection)) {
if (ret.contains(g)) throw new RuntimeException("Cycle Found");
ret.add(g);
}
}
}
return ret;
}
虫子很有可能会消失,但即使它没有,也会更容易追踪。