Maintaining a selection list?

I want to maintain a list of ships that the player currently has selected. This list would be what Actions sends commands to.

The simplest way to do this seems to be to have a static variable in my main Game class that is a list or array of the selected ships. My MousePick class would know how to add and subtract fromt the list. THen all Inputs and Actions can use that single list by accessing it statically, saving me a lot of time and confusion passing the current selection list around. But I wonder if this is safe and extensible?

Any thoughts? If it is safe, would a real array be OK? Or should I use a Collection?


There are several points to be addressed:

  • static

      makes it easy yes, but hinders extendability and is not really safe
  • list type

      I would recommend an ArrayList as long as you have few items that are selected (less than hundred) or seldom remove things from the list (for selection that is the case). Fiddling around with actual array is quite tiresome  ;). In cases where you have really a lot of items (thousands) and frequently remove items consider to use a HashSet, though it causes a lot of garbage and thus is not preferred if not really needed.
  • passing the selection list around.

      well, maybe think about reaching it another way depending on the use case…



    Hope this helps a bit…

Thanks. I'm going to stick with the ease of the static list for a while, but try to keep it open for a better method when I get around to really working out that bit.