How to create a hashmap for a specific object

拜拜、爱过 提交于 2019-12-10 11:31:39

问题


EDIT: @Oscar Lopez

I have added the code you specified, and now have the following: import java.util.HashMap; import java.util.Map;

public class Character{
    public String name;

    private HashMap<String, String> stats;

    public Character(String charName){
        name = charName;

        stats.put("Strength", "5");
        stats.put("Dexterity", "5");
        stats.put("Constitution", "5");
        stats.put("Intelligence", "5");
        stats.put("Strength", "5");
        stats.put("Wisdom", "5");
    }

    public String getStat(String name) {
        return stats.get(name);
    }

    public static void main(String[] arguments){
        Character tanis = new Character("Tanis");
        System.out.println(tanis.getStat("Dexterity"));             
    }
}

And it seems to be compiling correctly, but it doesn't like the way I am trying to stats.put things into the hashmap, how should I go about doing this?

import java.util.HashMap;

public class Character{
    public String name;


    private static HashMap<String, String> stats;

    public Character(String charName){
        name = charName;
        stats = new HashMap<String, String>();
        stats.put("Strength", "5");
        stats.put("Dexterity", "5");
        stats.put("Constitution", "5");
        stats.put("Intelligence", "5");
        stats.put("Strength", "5");
        stats.put("Wisdom", "5");
    }

    public String getStat(String statName) {
        return stats.get(statName);
    }

    public static void changeStat(Character character, String statName, String newStatValue) {
        character.stats.put(statName, newStatValue);
    }

    public static void main(String[] arguments){
        Character tanis = new Character("Tanis");
        System.out.println(tanis.getStat("Dexterity"));

        Character xander = new Character("Xander");
        changeStat(xander, "Dexterity", "7");
        System.out.println(xander.getStat("Dexterity"));    
        System.out.println(tanis.getStat("Dexterity"));                 
    }
}

//Prints out
//5
//7
//7

Why is tanis.getStat changing to 7?


回答1:


Declare the HashMap as an attribute in the Character class, then you can access it like you intend:

public class Character {
    private HashMap<String, String> stats;
    public String getStat(String name) {
        return stats.get(name);
    }
}

// elsewhere
Character tanis = new Character("Tanis");
System.out.println(tanis.getStat("Dexterity"));



回答2:


I'm not completely sure what you're trying to do, but I believe the best solution is to make a HashMap that is a member of your Character class. Thus, instead of String mapName, you could have HashMap<String, String> stats or something like that. Then, to access the map for tanis, you would say tanis.stats.




回答3:


You have not initialized the map - it is still null when you attempt to populate it.

Initialize it like this:

private Map<String, String> stats = new HashMap<String, String>();

With this change, your code will be OK.


Note how I have declared the map to be the abstract type Map, not the concrete implementation HashMap, in line with good programming practice.



来源:https://stackoverflow.com/questions/17159592/how-to-create-a-hashmap-for-a-specific-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!