为什么使用iterator()会给我错误,我也已经导入了包?

huangapple go评论70阅读模式
英文:

Why does using iterator() gives me error, I have also imported package?

问题

Iterator itr = m1.iterator();
while(itr.hasNext()){
    Map.Entry m = (Map.Entry)itr.next();
    if(m2.containsKey(m.getKey())){
        System.out.println(m2.containsKey(m.getKey()));
    }
}
Solution.java:34: 错误: 找不到符号
        Iterator itr = m1.iterator();
                     ^
  符号:   方法 iterator()
  位置: 类型为 Map<Character,Integer> 的变量 m1
1 错误
英文:
Iterator itr=m1.iterator();
    while(itr.hasNext()){
        Map.Entry m=(Map.Entry)itr.next();
            if(m2.containsKey(m.getKey())){
                System.out.println(m2.containsKey(m.getKey()));
            }
    }
Solution.java:34: error: cannot find symbol
        Iterator itr=m1.iterator();
                       ^
  symbol:   method iterator()
  location: variable m1 of type Map<Character,Integer>
1 error

答案1

得分: 2

因为 Map 没有 Iterator,但它有 entrySet()(它有一个 iterator())。就像这样,

Iterator<Map.Entry<Character, Integer>> iter = m1.entrySet().iterator();
英文:

Because Map doesn't have an Iterator, it does have an entrySet() (and that has an iterator()). Like,

Iterator&lt;Map.Entry&lt;Character, Integer&gt;&gt; iter = m1.entrySet().iterator();

答案2

得分: 0

Iterator itr=m1.iterator();应改为:

Iterator iter = m1.entrySet().iterator();

英文:

Iterator itr=m1.iterator(); should be this:

Iterator iter = m1.entrySet().iterator()

huangapple
  • 本文由 发表于 2020年10月13日 06:20:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64326045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定