我偶然发现了一些关于大学作业的编译器错误。经过一段时间的尝试修复后,我真的没有找到纠正它们的解决方案。
以下是实现非确定性有限自动机的程序的构造函数。在这种情况下,我正在使用Hashsets和Hashmaps。
public NFA(Set<String> states, Set<String[]> transitions, String start, Set<String> end) {
this.start = start;
this.end = end;
this.active = this.start;
Map<String, String> x = new HashMap<String, String>(); //create placeholder map
//Insert states into map
for (String s: states) {
this.states.put(s, x); //Placeholder map used for this line
}
//Assign transitions to corresponding states.
for (String[] t: transitions) {
//this line throws the error.
if (!states.containsKey(t[0]) || !states.containsKey(t[2])) {
throw new IllegalArgumentException("Transition Data Corrupted!");
} else {
this.states.get(t[0]).put(t[1], t[2]);
}
}
}
函数调用在我的readIn方法中,该方法从文件中提取NFA的构造数据。
result = new NFA(states, transitions, start, end);
javac宣布的编译器错误如下:
NFA.java:27: error: cannot find symbol if (!states.containsKey(t[0]) || !states.containsKey(t[2])) { ^ symbol: method containsKey(String) location: variable states of type Set<String> NFA.java:27: error: cannot find symbol if (!states.containsKey(t[0]) || !states.containsKey(t[2])) { ^ symbol: method containsKey(String) location: variable states of type Set<String>
此外,我从eclipse-ide中得到一个错误:
The method containsKey(String) is undefined for the type Set<String>
最佳答案
您需要使用contains(String value)
进行设置。在Set<T>
中,没有键值关系。
关于java - 非确定性有限自动机(containsKey(string)不兼容的HashMap错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355660/