3
\$\begingroup\$

I'm trying to make it so that no more than 5 units can be stacked on a tile. I have a Tile class that creates each tile's preferences. Should I store the units in an array or arraylist in this class?

\$\endgroup\$
6
\$\begingroup\$

Choosing a data structure will depend on what other features you want to support. In this case, you may as well use the ArrayList since it's easier to work with and more flexible.

It would be best to just store a reference to the unit in the tile, and have the unit objects stored elsewhere.

| improve this answer | |
\$\endgroup\$
  • 4
    \$\begingroup\$ Up to 5 units is so trivial though. Either will do. \$\endgroup\$ – Sidar Jan 13 '13 at 12:34
  • \$\begingroup\$ I agree @Sidar. I say ArrayList because it's much easier for a new developer to handle. With easy functions like add and remove. \$\endgroup\$ – MichaelHouse Jan 13 '13 at 20:02
0
\$\begingroup\$

Based on the nature of your question a Stack http://docs.oracle.com/javase/6/docs/api/java/util/Stack.html be optimal, however it is last in first out so I don't know if that would fit your requirements, otherwise you could also use a queue. If order doesn't matter to you then use ArrayList, it is generally the most efficient (good comparison to linkedlist here http://www.java-gaming.org/topics/arraylist-vs-linkedlist/27017/view.html)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.