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

时间:2015-11-11 15:49:31

标签: java collections

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

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

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);
}



}

1 个答案:

答案 0 :(得分:1)

您需要根据需要将这些以下语句放在方法或构造函数或某个静态块中,您将会很好。

List<MeleeWeapon> meleeList = MeleeWeapon.meleeWeaponList;

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