Я сделал следующие десятки раз:запись не определена в HashMap итерации цикла
for(Map.Entry<Key, WordSet> entry : topWordCountSets.entrySet()) {
Key currentKey = (entry.getKey());
}
Однако entry
в entry.getKey()
, по-видимому не определено. Я не определяю его нигде вне цикла, поэтому конфликтов нет.
Вот еще код (получить лучший алгоритм не завершен, из-за того, что запись не определена.):
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;
}
}
Какое сообщение об ошибке? – manouti
Какая IDE вы используете? –
Открыть/закрыть вашу среду IDE Clean/Build your Project – gtgaxiola