publicvoidbuy(Integerid,Stringtype){if("buy".equals(type)){if(hashtable.containsKey(id)){ShopItemitem=(ShopItem)hashtable.get(id);item.increment();}else{Foodfood=foodBiz....
public void buy(Integer id, String type) { if ("buy".equals(type)) { if (hashtable.containsKey(id)) { ShopItem item = (ShopItem) hashtable.get(id); item.increment(); } else { Food food = foodBiz.getFoodById(id); ShopItem item = new ShopItem(food); hashtable.put(id, item); } System.out.println("" + hashtable.size()); } else { System.out.println("buy" + hashtable.size()); hashtable.remove(id); System.out.println(hashtable.size()); } } 删除不了 hashtable里的对应id 的数据 由于我不知道你完整的程序是什么,所以没办法说你为什么删不掉 我学java的时候也遇到过这种情况 你可以这样删 先Set set=hashtable.keySet(); Iterator it= set.iterator (); while(it.hasNext()){ int a=next(); if(a==id){ it.remove(a); } } 就是先调用keySet方法返回一个hashmap的key的set视图, 然后调用set的迭代器,然后在迭代器中找到key,然后调用迭代器中的remove方法 这样绝对可以删掉了
展开