我的确定性图灵机不起作用,因为我的equals和indexof方法没有抛出源错误

时间:2016-05-26 17:35:27

标签: java equals hashcode

我有问题,我的equals方法不能正常工作。我想实现一个确定性的图灵机,所以我想添加方法findCommand(),它搜索命令的arraylist。所以我决定创建一个searchDummy来查找可用于我的配置的所有转换。

班级国家:

public class States {

private int stateId;
private boolean rejState;
private boolean accState;
private boolean stopState;
private List<Commands> commands = new ArrayList<Commands>();

在班级中等于:

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof States) {
        States otherState = (States) other;
        return (stateId == otherState.stateId);
    } else {
        return false;
    }
}

的hashCode:

@Override public int hashCode() {
    StringBuilder b = new StringBuilder(stateId);
    return b.toString().hashCode();
    }

这是各州的findCommand方法:

    public Commands findCommand(States state, char inputTapeChar, 
        char[] tapeChars) {
    Commands searchDummy = new Commands(state, inputTapeChar, tapeChars, 
            null, null, null, null);
    int pos = commands.indexOf(searchDummy);
    return pos >= 0 ? commands.get(pos) : null;
}

命令是我的arraylist,所以我想找到带有indexOf()的searchDummy。

我有类Commands,它包含属性Configuration配置,类Configuration,它包含Configuration的属性和属性Transition transition以及包含自身属性的类转换。

班级命令:

public class Commands implements Comparable<Commands> {

private Configuration configuration;

班级配置:

public class Configuration {

private Transition transition;
private States state;
private char inputTapeChar;
private char[] tapeChars;

班级转换:

public class Transition {

private States targetState;
private Direction inputTapeHeadMove;
private char[] newTapeChars;
private Direction[] tapeHeadMoves;

我在命令中有这个等于方法:

@Override public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Commands) {
Commands otherCmd = (Commands) other;
return (configuration.equals(otherCmd.configuration));
} else { 
return false; 
 }
}

和这个哈希码

    @Override
    public int hashCode() {
    StringBuilder b = new StringBuilder(configuration.getState() + "," 
    + configuration.getInputTapeChar());
    for (char c : configuration.getTapeChars()) {
        b.append("," + c);
    }
    return b.toString().hashCode();
    }

然后在配置中几乎相同:

    @Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof Configuration) {
        Configuration otherConfi = (Configuration) other;
        return (state.equals(otherConfi.state))
               && (inputTapeChar == otherConfi.inputTapeChar)
               && (Arrays.equals(tapeChars, otherConfi.tapeChars));
    } else {
        return false;
    }
}

哈希码:

@Override
public int hashCode() {
    StringBuilder b = new StringBuilder(state + "," + inputTapeChar);
    for (char c : tapeChars) {
        b.append("," + c);
    }
    return b.toString().hashCode();
}

类州的equales:

@Override
public boolean equals(Object other) {
    if (this == other) {
        return true;
    } else if (other instanceof States) {
        States otherState = (States) other;
        return (stateId == otherState.stateId);
    } else {
        return false;
    }
}

所以我的问题: 当我调试它时,它会一直运行直到它完成检查但是它应该返回它停留在Configuration.equals(...)的值并显示错误没有找到源代码!

有什么问题?哈希码是错的吗?或者等于错了吗?

我之前从未使用过equals,因此我不知道何时需要使用它或者我需要如何解决这个问题。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

您的hashCode实现看起来很可疑 - 所有String内容都不标准。

例如,您的Transition类应该是这样的:

@Override
public int hashCode() {
    int result = 17;
    result = 31 * result + targetState.hashCode();
    result = 31 * result + inputTapeHeadMove.hashCode();
    result = 31 * result + newTapeChars.hashCode();
    result = 31 * tapeHeadMoves.hashCode();
    return result;
}

大多数IDE都会提供hashCodeequals方法的自动生成。