An array filled with 2D arrays?

So I have maps made of 2D arrays like so:

int[][] level1 = {
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 0, 1, 1, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 1, 1, 2, 3, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}  //
	};

When I call the next level to be loaded, I access a getNextLevel class and pass the integer of the current level I am on. To make things easier on myself and not have to hard-code which level to load each time, I want to add allll of my levels into one array called Levels[], and then have my getNextLevel class look something like this:

public int[] getNextLevel(int level) {
		return levels[level];
	}

However, I could not get that to work. After researching on the idea, I couldn’t find anyone else who had run into the same problem. Thanks for helping :3

-Nathan

Since your levels are 2D arrays, shouldn’t getNextLevel also return a 2D array?

I guess so :l The part I am really flabbergasted on is whether my Levels array should be 2D or just a regular array. Since it is just holding a bunch of 2D arrays, I would think a regular array would work. However, when I try to make a new level and add it to Levels…

public int[] levels;
int[][] level1 = {
         {4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 0, 1, 1, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 1, 1, 2, 3, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}, //
			{4, 4, 4, 4, 4, 4, 4, 4, 4,}  //
	};

public void createLevels() {
		levels[1] = level1;
	}

…the “level1” in create levels says “Type mismatch: cannot convert from int[][] to int” even though it isn’t trying to use a standard integer anywhere. I even tried making levels a 2D array to match and it still said the same thing.

p.s. Want my source? Might help :l

-Nathan

levels[1] is especting an int, but you are giving an int[][] to it :frowning:

int levels[] means that levels is an array of integers, like this: [1][6][98][24], but level1 is a matrix, a 2 dimension array, like this:
[1][6][98][24]
[4][3][1][67]

Do you see it now? Hope it helps :slight_smile:

Yeah I gotcha. So there’s no way to do this? Sad_face.png

Yes you can, you have to create an array of 2d arrays, like your title:

int[][] levels[];

levels[1] now expects an array :slight_smile:

BTW I haven’t tested it, so… I’m not 100% sure, but it should work :slight_smile:

Wow, can’t believe I didn’t try that. Thanks good sire, worked perfectly :slight_smile:

-Nathan

You’re welcome :slight_smile:

Let us know about your progress!

Ok, side question: I am loading them all like this:

public void createLevels() {
		levels[1] = level1;
		levels[2] = level2;
		levels[3] = level3;
		levels[4] = level4;
	}

But is there some way to do it like this?

public void createLevel() {
		for (int i = 1; i < 5; i++) {
			levels[i] = level[i];
		}
	}

So what I basically want to do is look through all of my level integers and find the one that has “i” for the ending. Doesn’t seem possible, but maybe there is a way…

-Nathan

Object-oriented patterns would definitely simplify/clarify your code and make things easier in the long run. Reading maps from a file or image would also help if you plan to have many levels.

class Level {
    private int[][] tiles;

    public Level(int[][] tiles) { //or load it from a file just as easily
        this.tiles = tiles;
    }
}

List<Level> levels = new ArrayList<Level>();

public void init() {
    //create level 1
    levels.add(new Level( new int[][] { {0, 0, 0}, {1, 1, 1} .. etc } )); 

    //create level 2
    levels.add(new Level( new int[][] { {1, 1, 1}, {2, 2, 2} .. etc } )); 

    //starting level
    currentLevel = levels.get(0);
}

Wow. I wish I could hit appreciate 50 more times! I haven’t done anything object-oriented before myself, so this is a lot of new stuff for me to see. Thanks so much to both of you.

-Nathan

The important thing to notice about davedes’s code above is that the tiles array is private, and you should get at it with accessors. Don’t worry about performance, because if all your getTile(x,y) method does is return tiles[ x ][ y ], it’ll probably get inlined and be just as fast as direct access.