如何在声明时将项目添加到 Hashtable 中?

huangapple go评论79阅读模式
英文:

How to add item to Hashtable at Declaration?

问题

以下是你提供的代码的翻译部分:

所以我正在尝试创建一个基于文本的冒险/地牢探险游戏我正在尝试使用类似于我在基于文本的宝可梦游戏中用于项目怪物等存储的系统这里是我在C#中存储系统的功能代码

public static Dictionary<string, pokemonTrainer> pokemonTrainers = new Dictionary<string, pokemonTrainer>()
{
    {"Robert", new pokemonTrainer{name="Robert",typesOfPokemon={"Plant","Normal"}, levelMultiplier=0.4}},
    {"James", new pokemonTrainer{name="James",typesOfPokemon={"Plant","Normal","Water","Fire","Ice","Flying","Electric","Poison","Psychic","Fighting","Dark","Dragon","Fairy","Rock","Ghost","Ground","Bug"},levelMultiplier=0.8}}
};

我想创建一个哈希表其中包含特定的键值对就像C#代码中所做的那样而不仅仅是使用.put()来添加它们这可能吗如果可能的话我应该如何做如果不行创建这样一个存储系统的最佳方法是什么

如果有帮助的话这是我在Java中声明哈希表的方式

public static Hashtable<Integer, monster> monster_data_base = new Hashtable<Integer, monster>()
{
    
};
这是我想要作为键值对值部分的monster类

class monster
{
    private int health;
    private int damage;
    private String name;
    private int level;
    monster(int health, int damage, String name, int level)
    {
        this.health=health;
        this.damage=damage;
        this.name=name;
        this.level=level;
    }
    String get_name() 
    {
        return name;
    }
    int get_health() 
    {
        return health;
    }
    int get_damage() 
    {
        return damage;
    }
    void change_health(int change_in_durability) 
    {
        health=health+change_in_durability;
    }
    int get_level() 
    {
        return level;
    }
}
英文:

So I am trying to create a text-based adventure/dungeon crawler game. I am trying to use a similar system to what I used in a text-based Pokemon game for the storage of items, monsters, etc. Here is my functional code for the storage system in C#.

public static Dictionary&lt;string, pokemonTrainer&gt; pokemonTrainers = new Dictionary&lt;string, pokemonTrainer&gt;()
{
{&quot;Robert&quot;, new pokemonTrainer{name=&quot;Robert&quot;,typesOfPokemon={&quot;Plant&quot;,&quot;Normal&quot;}, levelMultiplier=0.4}},
{&quot;James&quot;, new pokemonTrainer{name=&quot;James&quot;,typesOfPokemon={&quot;Plant&quot;,&quot;Normal&quot;,&quot;Water&quot;,&quot;Fire&quot;,&quot;Ice&quot;,&quot;Flying&quot;,&quot;Electric&quot;,&quot;Poison&quot;,&quot;Psychic&quot;,&quot;Fighting&quot;,&quot;Dark&quot;,&quot;Dragon&quot;,&quot;Fairy&quot;,&quot;Rock&quot;,&quot;Ghost&quot;,&quot;Ground&quot;,&quot;Bug&quot;},levelMultiplier=0.8}}
};

I would like to create my hashtable with certain key-value pairs inside of it like what is being done in the C# code instead of just adding them with .put() is this possible? If so how would I do it? And if not what would be the best way to create a storage system like this?

If it helps at all here's my declaration for the hashtable in java,

public static Hashtable&lt;Integer, monster&gt; monster_data_base = new Hashtable&lt;Integer, monster&gt;()
{
};

and here's the monster class I would like to be the value part of the key-value pairs.

class monster
{
private int health;
private int damage;
private String name;
private int level;
monster(int health, int damage, String name, int level)
{
this.health=health;
this.damage=damage;
this.name=name;
this.level=level;
}
String get_name() 
{
return name;
}
int get_health() 
{
return health;
}
int get_damage() 
{
return damage;
}
void change_health(int change_in_durabilty) 
{
health=health+change_in_durabilty;
}
int get_level() 
{
return level;
}
}

答案1

得分: 1

创建一个静态方法来初始化你的静态散列表对象。这将在类加载时进行初始化。

public class Main {
    public static Hashtable<Integer, String> mytable = new Hashtable<>();

    static {
        initializeMytable();
    }

    // 静态初始化器,在这里你可以初始化你的静态对象
    static void initializeMytable() {
        mytable.put(2, "sam");
    }

    public static void main(String[] args) {

        // 在之后需要的地方使用你的表格
        System.out.println(mytable);
    }
}
英文:

Create a static method to initialize your static hashtable object. This will be initialized when the class is loaded.

public class Main {
public static Hashtable&lt;Integer,String&gt; mytable = new Hashtable&lt;&gt;();
static {
initializeMytable();
}
// static initializer where you can initiate your static object
static void initializeMytable() {
mytable.put(2,&quot;sam&quot;);
}
public static void main(String[] args) {
// use your table where you need it later
System.out.println(mytable);
}
}

huangapple
  • 本文由 发表于 2020年9月10日 06:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63820476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定