Note that there are some explanatory texts on larger screens.

plurals
  1. PORefactoring a class using ArrayList to use HashMap (Java)
    primarykey
    data
    text
    <p>I was told to change everything to HashMap() instead of ArrayList() and for the most part everything worked perfect. However, I am having a problem getting this one method to work properly.</p> <p>my HashMap() looks like</p> <p>private HashMap critMap = new HashMap();</p> <p>I have Room class and Creature class Room can have Creatures in it. The Creatures need to be able to react to certain commands which I already have methods for and should work as long as this method is right. I'm not certain to what is wrong. </p> <p>This is the method with ArrayList()</p> <pre><code>public void critReactRoomStateChange2(String command, PC pc, String name) { Creature temp = null; for (int i = 0; i &lt; critArr.size(); i++) { if (!(getCreatures().get(i) instanceof PC) &amp;&amp; !(getCreatures().get(i).getName().equals(name))) { temp = getCreatures().get(i); if (temp != null) { getCreatures().get(i).reactStateChange(command, pc); temp.checkNewRoom(); if (!temp.equals(getCreatures().get(i))) { i--; } } } } } </code></pre> <p>THIS IS THE METHOD AFTER I TIRED TO IMPLEMENT HashMap()</p> <pre><code>public void critReactRoomStateChange(String command, PC pc, String name) { Creature temp = null; if (!(getCreatures().get(name) instanceof PC)) { temp = getCreatures().get(name); if (temp != null) { getCreatures().get(name).reactStateChange(command, pc); temp.checkNewRoom(); } } } </code></pre> <p>the getCreatures().get(name) is taking the String name that is passed to it as a key of the hashMap to find the actual object it is referring to. As stated above my hashMap is thus the creatures name is the String(key) and the value Creature(contains other information other than name) is the value. When I call getCreature().get(name) I am looking for the Key String name and I want it to return the object Creature. If it cannot find it in the hashMap it should return null unless I'm mistaken. </p> <p>I might just be missing something really simple. Any help would be greatly appreciated. If more code is needed I'd gladly edit this and put it in.<br> Thanks</p> <p>Edit: Creature class is a is abstract and PC, Animal, NPC all extend it. Just so your not wondering what the random PC and NPC and Animals are doing. lol </p> <p>Edit2: No error besides that I'm not getting the reactions. It does nothing so critReactRoomStateChange is not working now. The Creatures are not getting passed along so the other methods can act on it. </p> <p>So the 2nd box of code isn't working properly. It does nothing essentially. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload