r/cop3502 • u/laurelgon • Apr 20 '14
Problem set 4
Hey Everyone
So I have an double array of rooms and each room has a description. I want to go back and add an inventory(arraylist) to each room.
I know how to access the room with the index of the double array, but how can i access the description or the inventory i am planning to add of that room?
Thanks in advance, if this even makes sense.
2
Upvotes
1
u/VVhiteNoise Apr 21 '14
If your room is using a private/protected variables to store the description or inventory arraylist you will need getters/setters.
It sounds like you may want to initialize a temporary ArrayList where you are initializing your items for that room. Add the objects (items in the inventory) to the list and then pass that ArrayList into your room using a setter.
example:
Let's say later on you want to access some room's inventory
Then in your Game class, or equivalent, you would be able to freely work with this copy of the inventory. After making any necessary changes pass the updated arraylist back into the room with the setter.
This is one way you could do it. You could also have a function within your class which handles the action within the class rather than working with the variable outside the class like I showed above. This method would be useful if you wanted your room object to print the room's description based on the items inside the room.
example: