0

嘿,我现在在我的程序的最后润色中,我遇到了最奇怪的错误,基本上我正在比较两个都包含两个字符串的对象,所以我正在比较字符串的所有组合以查看对象是否相关通过一个共同的字符串。

它是用于 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 BC BD AD CG BF GF CF,其中 label1 为 a,第一个为 label 2 c,它只发生在这两个上,当然,给我一个无效的结果。

4

1 回答 1

0

Your code is very repetitive, which makes it exposed to such crazy bugs. Why don't you make the followinrg additions:

In Node:

String normLabel() { return getLabel().toUpperCase(); }

In Connection:

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);
  }

Then your main code can collapse into the following:

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;
}

There's a good chance the bug will just evaporate, but even if it doesn't, it'll be much easier to track down.

于 2012-04-22T21:04:06.793 回答