【发布时间】:2014-11-26 15:45:55
【问题描述】:
我已经做了几十次了:
for(Map.Entry<Key, WordSet> entry : topWordCountSets.entrySet()) {
Key currentKey = (entry.getKey());
}
但是,entry.getKey() 中的 entry 显然是未定义的。我没有在循环之外的任何地方定义它,所以没有冲突。
这里有更多代码(get best算法没有完成,因为entry没有定义。):
package hangman;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
public class Partitions {
private int _wordLength = 2;
private WordSet _L;
private HashMap<Key, WordSet> _partitions = new HashMap<Key, WordSet>();
public void initialize(InputStream stream, int wordLength) throws Exception {
_L = WordSetParser.generate(stream,wordLength);
}
private void _partition(char by) throws Exception {
_partitions.clear();
Iterator<Word> iterator = _L.iterator();
while(iterator.hasNext()) {
Word nextWord = iterator.next();
if(nextWord.length() == _wordLength) {
Key nextKey = new Key(nextWord, by);
if(!_partitions.containsKey(nextKey)) {
_partitions.put(nextKey, new WordSet(_wordLength));
}
_partitions.get(nextKey).add(nextWord);
}
}
}
public WordSet getBestPartition(char by) throws Exception {
//Establish partitions
_partition(by);
//Find partitions with greatest number of words
HashMap<Key, WordSet> topWordCountSets = new HashMap<Key, WordSet>();
int maxWords = 0;
for(Map.Entry<Key, WordSet> entry : _partitions.entrySet()) {
if(entry.getValue().size() > maxWords) {
maxWords = entry.getValue().size();
topWordCountSets.clear();
topWordCountSets.put(entry.getKey(), entry.getValue());
}
else if(entry.getValue().size() == maxWords) {
topWordCountSets.put(entry.getKey(), entry.getValue());
}
}
if(topWordCount.size() == 1)
return (WordSet)topWordCountSets.values().toArray()[0];
else {
//Find partitions with best key
Key bestKey = null;
for(Map.Entry<Key, WordSet> entry : topWordCountSets.entrySet()) {
Key currentKey = (entry.getKey());
if(bestKey == null)
bestKey = currentKey;
else if(currentKey.count() == 0 && bestKey.count() != 0)
bestKey = currentKey;
else if(currentKey.count() != bestKey.count())
bestKey = () ? bestKey : currentKey;
}
}
return null;
}
}
【问题讨论】:
-
确切的错误信息是什么?
-
你用的是什么IDE?
-
打开/关闭您的 IDE 清理/构建您的项目
-
请展示一个简短但完整的程序来说明问题。另请注意,
entry.getKey()周围的括号是没有意义的。 -
我在使用Eclipse Luna,错误是'entry cannot be resolve',询问我是否要创建一个带有名称或字段的局部变量,我已经尝试清理和构建,并且我意识到多余的括号是没有意义的,它们只是剩下的。稍后我会发布更多代码。