gpt4 book ai didi

Java - 将静态 arrayList 从一个类复制到另一个类中的 HashMap

转载 作者:行者123 更新时间:2023-12-01 10:55:00 26 4
gpt4 key购买 nike

这个问题真的非常简单,但我不知道为什么 stackoverflow 中类似问题的建议解决方案在我的代码中不起作用。

我在MeleeWeapon类中有一个静态meleeWeaponList列表,我想将列表的内容复制到另一个类(Blacksmith)中的HashMap(键)。问题可能出在铁匠课的最后三行。我应该解决什么问题?非常欢迎任何有关如何处理数据的建议

public class  MeleeWeapon extends Weapon {

boolean throwable;

MeleeWeapon(String name,boolean oneHaned, String reqTraining, int n, int dice, int attackBonus, int damageBonus,double weight, long cost, boolean throwable) {
super(name, weight, cost, oneHaned, reqTraining, n, dice, attackBonus, damageBonus);
this.throwable = throwable;
}

static List<MeleeWeapon> meleeWeaponList = new ArrayList<MeleeWeapon>();

static
{
meleeWeaponList.add(new MeleeWeapon("Long Sword",true, "Martial", 1, 8, 0, 0,8, 10, false));
meleeWeaponList.add(new MeleeWeapon("Short Sword",true, "Martial", 1, 6, 0, 0,5, 5, false));
meleeWeaponList.add(new MeleeWeapon("Dagger",true, "Basic", 1, 4, 0, 0,2, 3, true));
meleeWeaponList.add(new MeleeWeapon("Quarter-staff",false, "Basic", 1, 4, 0, 0,3, 2, false));
meleeWeaponList.add(new MeleeWeapon("Shield",false, "Martial", 1, 4, 0, 0,8, 8, false));

}


public void attack(Character attacker, Character defender){

int attackRoll = DiceRoller.roll(20) + attacker.getBaseAttackBonus() + attacker.getModifier(attacker.getStrength()) + getAttackBonus() ;
System.out.println(attacker.getName() + " attack Roll: " + attackRoll + "AC: " + defender.getArmorClass());

if (attackRoll >= defender.getArmorClass()){
System.out.println("Defender: " + defender.getName() + " had " + defender.getCurrentHp());
int damage = DiceRoller.roll(getN(), getDice()) + attacker.getModifier(attacker.getStrength()) + getDamageBonus() ;
System.out.println("Damage : " + damage);
defender.setCurrentHp(attacker.getCurrentHp() - damage);
System.out.println("Defender: " + defender.getName() + " has " + defender.getCurrentHp());
} else {
System.out.println("Missed Attack");
}

}

public static MeleeWeapon getItem(String itemName) {
try {
for (Iterator<MeleeWeapon> iter = meleeWeaponList.iterator(); iter.hasNext(); ) {
MeleeWeapon item = iter.next();
if (itemName.equals(item.getName())) {
return item;
}
}

} catch (Exception e){
System.out.println(itemName + " haven't been found");
return null;
}
return null;
}


}



public class Blacksmith {

private Map<MeleeWeapon,Integer> meleeMap;
private Map<RangedWeapon,Integer> rangedMap;
private Map<Armor,Integer> armorMap;

Blacksmith(){

meleeMap = new HashMap<MeleeWeapon, Integer>();
rangedMap = new HashMap<RangedWeapon, Integer>();
armorMap = new HashMap<Armor, Integer>();
}

List<MeleeWeapon> meleeList = MeleeWeapon.meleeWeaponList;

for (MeleeWeapon weapon : meleeList) {
meleeMap.put(weapon, 5);
}



}

最佳答案

您需要根据需要将以下语句放入方法或构造函数或某个静态 block 中,这样就可以了。

List<MeleeWeapon> meleeList = MeleeWeapon.meleeWeaponList;

for (MeleeWeapon weapon : meleeList) {
meleeMap.put(weapon, 5);
}

关于Java - 将静态 arrayList 从一个类复制到另一个类中的 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com