views:

141

answers:

3

I'm currently creating a tile based game, where elements of the games are placed in four different vectors (since there are multiple game objects with different properties, hence stored in different vectors).

These game elements themselves contain x and y coordinates similar to how they are stored in a two dimensional array. i was wondering if there was a way to access these vector elements similar to two dimensional array access (currently i am implementing an for loop to cycle the elements while comparing its coordinates).

This kinda sucks when i need to refresh my display at every game cycle (since the large number of comparisons and loops).

I'm implementing this in java btw.

+4  A: 

My recommendation would be to think "object-oriented": create a class named Board or Grid or whatever fits that encapsulates that implementation detail of choosing between 2D array or Vector of Vectors. Add a method that lets you return a board token for a given (i, j) index into the board.

duffymo
Except please don't use Vector unless you know why you need it.
Vuntic
Agreed. If s/he encapsulates all this into a class, nobody has to know what the implementation is. It can be swapped for ArrayList or anything else.
duffymo
there are instances when the vector is not completely full (because of the layered approach, a cell in one vector may not contain an object, but the same cell at a different vector may contain an object). from what i know we cant store null values into vectors. im considering the use of a 2d array (and simply storing null values where necessary), though im not quite so sure with it.
mixm
+2  A: 

Don't use Vector, use ArrayList.

If you have very large datas, see perhaps buffers, for instance IntBuffer.

Istao
+1 Vector should (almost) never be used.
Vuntic
A: 

Three Ideas:

  • You could use a HashMap with the coordinates as key and your elements as value. This is faster than cycling through the vector and lightweight for memory.
  • You could store null instead of elements at empty coordinates. This way you can access each stored memory by its coordinates.Fastest but memory intensive way.
  • A speed up for what you currently do: Sort your elements by their coordinates once and then use binary search to find them in the vector.
josefx
What?...how is a HashMap faster?
Vuntic
@Vuntic "how is a hashmap faster?" - by orders of magnitude. Java hashmaps give you constant time access ( O(1) ), while linear collections like arraylists and vectors are searchable at O(n). Oh, and then there's the fact that Vector is inefficiently synchronized. josefx's solution is a large improvement.
CPerkins
Ohhh I see what you're doing. Yeah, it would be a lot faster then. I was thinking that you had a 2D grid into which the objects were put - with HashMap, you have objects with 2D coordinates. If object density is high, the latter is a pretty inefficient way to do things.
Vuntic
@Vuntic - to be fair, it's not what _I'm_ doing. It was josefx's solution. I just answered you about HashMap, and no, hashmaps are not inefficient. There's a small overhead of memory, but the lookup time for an array of arrays (your 2d grid) is roughly the same as the lookup time for a hashmap keyed by the coordinates, whether object density is high or low. But basically I'm with duffymo: make a Board (or Grid, or whatever) class, and keep the representation of objects internal to that.
CPerkins
@CPerkins Ah. Sorry, didn't look at the name. I guess it would be pretty much the same that way, because when you do it like that it's basically the same data structure, but that seems kinda... pointless.
Vuntic
@Vuntic Pointless? I dunno, it accomplishes the same goal, in what's arguably simpler. But if we follow duffymo's solution and encapsulate, only the implementor of Board(Grid, whatever) has to deal with the issue - callers can think of it as per-grid-coordinates.
CPerkins