# Datastructure for infinite 2D map [duplicate]

I started a 2D Tilegame programmed with libgdx which needs potentially infinite tiles. That means when the player arrives the border of the map it should be possible to generate even more tiles. So a simple 2D Array is not the best datastructure for that purpose. A better solution I found here in the comments from dlras2: A way to store potentially infinite 2D map data?

it seems like he put 2D arrays in 2D arrays and that again and again, that reduces a lot of memory compared to one array for all tiles:

The problem is that I dont know before how many times the arrays will be encapsulated so it should be easily possible to wrap a new Array around if it is needed.

dlras2 also posted his code in C#, but I dont understand anything from it. Do you have an idea how to implement such a datastructure of recursive Arrays in Java? Or how to translate the whole C# code into Java? I hope the other stuff like putting tiles in the right array I'll solve by my selve.

• What you have there are chunks that are represented as quad-trees. You're question here appears to be asking us to explain how a C# demo might be converted into Java. This is not a game development specific task and not on topic for the stack exchange network. – MichaelHouse Feb 16 '15 at 22:45
• (necro to add some more brainfood) way to store potentially infinite map data? - DON'T. There's no need to. Make a deterministic generation algorithm that generates chunks based on unique chunk seed created for each from its x+y pos+global map seed. that way all you need to save is the initial seed, and list of player's changes to the map. then at runtime, you can (re)generate any chunk at any time, then apply player changes, and the result is "loaded" chunk. upside is that regardless of the size of the map, its base savefile size will always be only the size of initial seed. – sh code May 29 '17 at 8:20

Here is a possible solution.

Let us define a segment of map as a fixed size 2D array; 10 by 10 in this example.

Then let us use a hashmap (or whatever the equivalent is in Java) that keep track of the right segment to use. For example,

function getTile(x,y):
segmentI = int(x/10);
segmentJ = int(y/10);
segmentToUse = hashmap[segmentI+segmentJ];
if (segmentToUse doesnt exist) create new map segment, put into hashmap
return segmentToUse[x%10][y%10];