【问题标题】:Entry undefined in HashMap iteration loopHashMap 迭代循环中未定义的条目
【发布时间】: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',询问我是否要创建一个带有名称或字段的局部变量,我已经尝试清理和构建,并且我意识到多余的括号是没有意义的,它们只是剩下的。稍后我会发布更多代码。

标签: java loops map iterator


【解决方案1】:

您的三元运算符中有一个错误,使编译器感到困惑:

bestKey = () ? bestKey : currentKey;

你忘了在括号内加上条件。

我通过一个小例子设法重现了这个错误(我实际上使用了一个具有不同类型的 Key 和 Value 的映射,但没有区别):

  for (Map.Entry<Key, WordSet> entry : topWordCountSets.entrySet ()) {
    Key currentKey = entry.getKey (); // fake error
    Key bestKey = () ? null : null; // real error
  }

这会使编译器显示两个错误,一个是entry,一个是()。如果您修复了() 错误(例如,将其更改为(true)),这两个错误都会消失。我不知道为什么编译器会在entry 上报告错误。

【讨论】:

  • 奇怪...我尝试了同样的方法,但在 bestKey = () 处只得到一个错误?最佳密钥:当前密钥;带有 openjdk 的 Arch Linux。知道 OP 有哪个 Java 版本会很有趣
  • @user 我不知道,但我在 Eclipse Juno 上的 Java 6 上重新创建了它。
  • Bonks 用头反复撞墙 哇,谢谢。我什至没有想过再看下去。我原本预计只会在三元中出现编译错误。
猜你喜欢
  • 1970-01-01
  • 2014-10-31
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 2019-07-28
  • 2011-08-14
  • 2015-05-02
相关资源
最近更新 更多