英文:
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<Map.Entry<Character, Integer>> 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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论