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

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

How to add item to Hashtable at Declaration?

问题

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

  1. 所以我正在尝试创建一个基于文本的冒险/地牢探险游戏我正在尝试使用类似于我在基于文本的宝可梦游戏中用于项目怪物等存储的系统这里是我在C#中存储系统的功能代码
  2. public static Dictionary<string, pokemonTrainer> pokemonTrainers = new Dictionary<string, pokemonTrainer>()
  3. {
  4. {"Robert", new pokemonTrainer{name="Robert",typesOfPokemon={"Plant","Normal"}, levelMultiplier=0.4}},
  5. {"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}}
  6. };
  7. 我想创建一个哈希表其中包含特定的键值对就像C#代码中所做的那样而不仅仅是使用.put()来添加它们这可能吗如果可能的话我应该如何做如果不行创建这样一个存储系统的最佳方法是什么
  8. 如果有帮助的话这是我在Java中声明哈希表的方式
  9. public static Hashtable<Integer, monster> monster_data_base = new Hashtable<Integer, monster>()
  10. {
  11. };
  12. 这是我想要作为键值对值部分的monster
  13. class monster
  14. {
  15. private int health;
  16. private int damage;
  17. private String name;
  18. private int level;
  19. monster(int health, int damage, String name, int level)
  20. {
  21. this.health=health;
  22. this.damage=damage;
  23. this.name=name;
  24. this.level=level;
  25. }
  26. String get_name()
  27. {
  28. return name;
  29. }
  30. int get_health()
  31. {
  32. return health;
  33. }
  34. int get_damage()
  35. {
  36. return damage;
  37. }
  38. void change_health(int change_in_durability)
  39. {
  40. health=health+change_in_durability;
  41. }
  42. int get_level()
  43. {
  44. return level;
  45. }
  46. }
英文:

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#.

  1. public static Dictionary&lt;string, pokemonTrainer&gt; pokemonTrainers = new Dictionary&lt;string, pokemonTrainer&gt;()
  2. {
  3. {&quot;Robert&quot;, new pokemonTrainer{name=&quot;Robert&quot;,typesOfPokemon={&quot;Plant&quot;,&quot;Normal&quot;}, levelMultiplier=0.4}},
  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}}
  5. };

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,

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

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

  1. class monster
  2. {
  3. private int health;
  4. private int damage;
  5. private String name;
  6. private int level;
  7. monster(int health, int damage, String name, int level)
  8. {
  9. this.health=health;
  10. this.damage=damage;
  11. this.name=name;
  12. this.level=level;
  13. }
  14. String get_name()
  15. {
  16. return name;
  17. }
  18. int get_health()
  19. {
  20. return health;
  21. }
  22. int get_damage()
  23. {
  24. return damage;
  25. }
  26. void change_health(int change_in_durabilty)
  27. {
  28. health=health+change_in_durabilty;
  29. }
  30. int get_level()
  31. {
  32. return level;
  33. }
  34. }

答案1

得分: 1

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

  1. public class Main {
  2. public static Hashtable<Integer, String> mytable = new Hashtable<>();
  3. static {
  4. initializeMytable();
  5. }
  6. // 静态初始化器,在这里你可以初始化你的静态对象
  7. static void initializeMytable() {
  8. mytable.put(2, "sam");
  9. }
  10. public static void main(String[] args) {
  11. // 在之后需要的地方使用你的表格
  12. System.out.println(mytable);
  13. }
  14. }
英文:

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

  1. public class Main {
  2. public static Hashtable&lt;Integer,String&gt; mytable = new Hashtable&lt;&gt;();
  3. static {
  4. initializeMytable();
  5. }
  6. // static initializer where you can initiate your static object
  7. static void initializeMytable() {
  8. mytable.put(2,&quot;sam&quot;);
  9. }
  10. public static void main(String[] args) {
  11. // use your table where you need it later
  12. System.out.println(mytable);
  13. }
  14. }

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:

确定