Reputation: 18200
Let's say I have these monsters (already configured their names, attack, level, exp, etc) but what's a good way to keep track of their X and Y?
Maybe an ArrayList<>
? Like add in that arraylist 1|12|8|3
where 1
is the ID of Monster (to later use as m1
.gif, 12 is X and 8 is Y and 3 is map number.
What do you think is a good way to store monsters in the map in Java?
Upvotes: 1
Views: 5669
Reputation: 72748
What you describe, simply storing a few int
s, is a code smell called "Primitive Obsession".
The generally better solution is coming up with an object to mean what you want it to mean. For example, you can have a Map
where the keys are a monster and the values are a MapPoint
s (X, Y and map ID).
In general, you should start thinking in objects and naming these concepts you want to use.
Good luck!
Upvotes: 5
Reputation: 916
It would be best to create a 'Monster' class that will hold your variables.
A quicker duck tape way would be to use a regular array instead of an ArrayList if you're only storing four variables.
int[] monster1 = { 1, 12, 8, 3 };
And to access them:
int id = monster1[0];
Upvotes: 1
Reputation: 7397
Well you certainly wouldn't want to take advantage of Encapsulation by using Objects in an Object Oriented Language.
class Monster
{
private String myName;
private int attack;
private int level;
private int experience;
public Monster(String n, int att, int lvl, int e)
{
myName = n;
attack = att;
level = lvl;
experience = e;
}
// other useful methods go here!
}
Upvotes: 7