![](/img/trans.png)
[英]ConcurrentModificationException while iterating through HashMap
[英]Error while iterating through HashMap in Java
我試圖像這樣通過HashMap進行迭代:
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
}
但是我得到這個錯誤:
entry cannot be resolved
這是錯誤的方法嗎? 據我所知,這似乎對其他人有用。
您應該使用Iterator
。 查看文檔
您需要導入java.util.Map
嘗試這個
for(Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator(); it.hasNext();)
{
Map.Entry<String,Integer> entry = it.next();
String key = entry.getKey();
Integer value = entry.getValue();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.