我正在制作一个虚拟动物园。我的动物园有一个食品店,其中包含一系列食品和每种食品的数量。这里是我foodstore类:如何将项目从一个哈希映射移动到另一个哈希映射?
public class Foodstore {
Map<String, Integer> register = new HashMap<String, Integer>(); //hashmap called register declaration
public void addFood(String food, int quantity) {
if (register.containsKey(food)) { //if register contains this food
Integer newAmount = register.get(food) + quantity; //assign new amount to newAmount
register.put(food, newAmount); //add this amount of the type of food
}
else {
register.put(food, quantity);
}
}
public void takeFood(String food) {
if (register.containsKey(food)) { //if register contains this food
Integer newAmount = register.get(food); //assign new amount to newAmount
register.remove(food, newAmount); //remove this amount of food
}
else {
register.remove(food);
}
}
}
我也有包含自己的食品商店的防护等级,这里是我创建的外壳食品店的HashMap
Foodstore store = new Foodstore();
在我动物园我有动物园管理员谁拥有aMonthPasses方法,需要允许他们从动物园foodstore移动多达20个项目到圈地食品商店。我想知道如何去做这件事?谢谢
在'java.util.HashMap'没有方法'删除(,)'。 –
hotzst