# Java map generation

What is better way of setting up a map for a java 2D topdown game? Would it be having an ArrayList of tiles, a simple 2D array, a HashMap? (Note: the tiles/blocks or whatever are squared)

## 2 Answers

It depends on what you need to store. If you just want the positions of the tiles, a 2D array is probably the easiest way to store it (unless you have a dynamic world size). If you want the connections (for movement costs, etc.) then a Graph is probably what you want to use.

Usually a single one dimensional array would work fine, just remember that, to get the tile you want you have to use the formula tile[x +(y*tiles_in_row)]

• While 1D works 2D is easier on the developer in terms of actual code. It's a lot easier to write [x][y] than to write [x +(y*width)] and the performance impact is exceedingly minimal. – Draco18s no longer trusts SE Jan 18 '16 at 18:17
• If you want to store an 8bit integer for cell in a 255^2 map the memory usage and performance will be near the same in a 1D or a 2D array, but in a bigger maps (I mean, huge), the performance will decrease in a 2D array, iterations over large 1D arrays are more efficient than looping through dimensions – PRDeving Jan 18 '16 at 20:59
• Ay what sufficiently large values of huge are you referring to? That is: how large does the array need to be before the performance makes a noticeable impact? – Draco18s no longer trusts SE Jan 18 '16 at 21:03
• The difference is apreciable since you set a 2^2 map, but, if you set a 2048^2 map f.e. you can see how performance drops, won't be a big dropdown but its there, and added to other performance issues, could steal some fps to your final product – PRDeving Jan 18 '16 at 21:07