簡體   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