【发布时间】:2013-10-16 03:53:51
【问题描述】:
我正在尝试访问我放入哈希图中的内容,但它不起作用。 显然 hashmap 的迭代器没有任何东西。它不能做一个mapIter.hasNext(),它会是假的。
代码如下:
Iterator<Product> cIter = getCartContent(cart).iterator();
HashMap<Product, Integer> hash = new HashMap<Product, Integer>();
Iterator<Product> mIter = hash.keySet().iterator();
Product p;
while(cIter.hasNext()) {
p = cIter.next();
if(hash.containsKey(p))
hash.put(p, hash.get(p) + 1);
else
hash.put(p, 1);
}
if(!mIter.hasNext())
System.out.println("Empty mIter");
【问题讨论】:
-
然后向我们展示更大的图景。向我们证明其中有元素。发布 SSCCE。
-
使用 hashmap.isEmpty() 返回 false。抱歉,我是新手。。
-
您的代码中有大量变量,您没有向我们展示它们是如何创建的,也没有评论来解释它们。例如,
getCartContent是什么?你确定问题不在那个方法上吗?