[英]my deterministic turing machine won't work, because my equals and indexof method throw no source error
I have the problem, that my equals method doesnt work as i want it to.我有一个问题,我的 equals 方法不能像我想要的那样工作。 I want to implement a deterministic turing machine, so I want to add the method findCommand(), which searchs through a arraylist of commands.
我想实现一个确定性图灵机,所以我想添加方法 findCommand(),它搜索命令数组列表。 So I decided to create a searchDummy to find all Transitions that are available for the Configuration I have.
因此,我决定创建一个 searchDummy 来查找可用于我拥有的配置的所有转换。
Class States:类状态:
public class States {
private int stateId;
private boolean rejState;
private boolean accState;
private boolean stopState;
private List<Commands> commands = new ArrayList<Commands>();
equals in class States:等于类状态:
@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();
}
this is the findCommand method in States:这是状态中的 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;
}
commands is my arraylist, so I want to find the searchDummy with indexOf().命令是我的数组列表,所以我想用 indexOf() 找到 searchDummy。
I have the class Commands, which holds the attribute Configuration configuration, the class Configuration, which holds the attributes of a Configuration and the attribute Transition transition and the class transition that holds the attributes for itself.我有类 Commands,它保存属性 Configuration 配置,类 Configuration,它保存 Configuration 的属性和属性 Transition 转换以及保存自身属性的类转换。
Class Commands:类命令:
public class Commands implements Comparable<Commands> {
private Configuration configuration;
Class Configuration:班级配置:
public class Configuration {
private Transition transition;
private States state;
private char inputTapeChar;
private char[] tapeChars;
Class Transition:类转换:
public class Transition {
private States targetState;
private Direction inputTapeHeadMove;
private char[] newTapeChars;
private Direction[] tapeHeadMoves;
i have this equals method in Commands:我在命令中有这个 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;
}
}
and this hashcode和这个哈希码
@Override
public int hashCode() {
StringBuilder b = new StringBuilder(configuration.getState() + ","
+ configuration.getInputTapeChar());
for (char c : configuration.getTapeChars()) {
b.append("," + c);
}
return b.toString().hashCode();
}
then almost the same in Configuration:然后在配置中几乎相同:
@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;
}
}
hashcode:哈希码:
@Override
public int hashCode() {
StringBuilder b = new StringBuilder(state + "," + inputTapeChar);
for (char c : tapeChars) {
b.append("," + c);
}
return b.toString().hashCode();
}
equales in class State:等于类状态:
@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;
}
}
so my question: when I debug this it goes through until it's finished with the checks but when it should return the value it stucks at Configuration.equals(...) and shows the error no source found!所以我的问题是:当我调试它时,它会一直持续到检查完成,但是当它应该返回值时,它会停留在 Configuration.equals(...) 并显示没有找到源的错误!
what is the problem?问题是什么? Are the hashcodes wrong?
哈希码是错误的吗? Or are the equals wrong?
还是等号错了?
I never used equals before so I dont know when i need to use it or how i need to fix this.我以前从未使用过 equals,所以我不知道什么时候需要使用它或我需要如何解决这个问题。 thanks for your help.
谢谢你的帮助。
Your hashCode
implementation looks suspect - all that String stuff is not standard.您的
hashCode
实现看起来很可疑 - 所有 String 的东西都不是标准的。
For example for your Transition class should be something like this:例如,您的 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;
}
Most IDEs will offer autogen of hashCode
and equals
methods.大多数 IDE 将提供
hashCode
和equals
方法的自动生成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.