Use ArrayList or map[x][y] for a large map-grid

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Use ArrayList or map[x][y] for a large map-grid

Postby Frepo » Thu Jul 05, 2018 11:10 am

Hi everyone!

I'm making a classic turn-based 4x-game. Which implements a large tile-based map.
I've been trying to make up my mind if I should use an ArrayList or a 2-dimensional indexed variable for the map. Performance-wise is there any difference?

Each map tile is an object. So the map will be defined as either of these two options:

1. ArrayList<AbstractTile> map;
2. AbstractTile[][] map;

Would either of these two be recommended before the other? Note that:
- Map size will never change (after game start)
- Map size can be very large (216 x 144 = 31104 tiles, perhaps even larger if I decide to add another mapsize-option)
- To save memory I want to minimize how much data is stored in each tile. Eg I don't want to store an index-id since that can be calculated at run-time.

Observation: Using an ArrayList would require a lot of conversions from Tile X- Y-coords to array index position (eg. to acquire a tile based on mouse position).
Which could look something like:
Code: Select all
mouseOverTileX = mousePos.x / tileSizeX;
mouseOverTileY = mousePos.y / tileSizeY;
index = mouseOverTileX + (mouseOverTileY * mapSizeX)

Meaning using map[x][y] would not require the last line of code there (the conversion to ArrayList-index).

Any thoughts on this? Thanks.
Frepo
 
Posts: 10
Joined: Sun Dec 31, 2017 1:21 pm
Location: Sweden

Re: Use ArrayList or map[x][y] for a large map-grid

Postby Magnesus » Fri Jul 06, 2018 11:06 am

Since the map size doesn't change I see no reason to use ArrayList. Go with a normal array variable, they are made for this.
Magnesus
 
Posts: 1614
Joined: Sun Sep 25, 2011 3:50 pm

Re: Use ArrayList or map[x][y] for a large map-grid

Postby Frepo » Sun Jul 08, 2018 11:14 am

Thanks! I came to the same conclusion myself. Much nicer to work with the map that way, finding tiles as map[x][y] is easy to read. Not to mention looping through, for example, a rectangular area of the map.
Frepo
 
Posts: 10
Joined: Sun Dec 31, 2017 1:21 pm
Location: Sweden


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest