繁体   English   中英

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

[英]my deterministic turing machine won't work, because my equals and indexof method throw no source error

我有一个问题,我的 equals 方法不能像我想要的那样工作。 我想实现一个确定性图灵机,所以我想添加方法 findCommand(),它搜索命令数组列表。 因此,我决定创建一个 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;
    }
}

哈希码:

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

命令是我的数组列表,所以我想用 indexOf() 找到 searchDummy。

我有类 Commands,它保存属性 Configuration 配置,类 Configuration,它保存 Configuration 的属性和属性 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;

我在命令中有这个 equals 方法:

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

等于类状态:

@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,所以我不知道什么时候需要使用它或我需要如何解决这个问题。 谢谢你的帮助。

您的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方法的自动生成。

暂无
暂无

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

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