5

With my Tile Editor that I created I get an Array like this:

int [][] Level02 = new int[][]  {
                            { 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12}, 
                            { 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 12}, 
                            { 11, -1, -1, -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, -1, -1, 13, -1, -1, -1, -1, 12}, 
                            { 11, 13, -1, -1, 27, 27, 27, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, -1, -1, -1, -1, -1, 13, 13, -1, -1, -1, 13, 13, -1, -1, -1, 32, -1, -1, 27, 27, 25, 25, 27, 27, -1, -1, 32, 12}, 
                            { 16, 16, 16, 16, 16, 16, 16, 16, 16, -1, -1, 13, 13, -1, -1, -1, 13, -1, -1, -1, 25, 25, 25, -1, -1, -1, 27, 27, 27, 27, -1, -1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16}, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 16, 16, 16, 16, 16, 1, 1, 1, 1, 1, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
                            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
                        }

So it prints out a 2 dimensional array.

The problem is that I have hundreds of these in one class and what to organize them to be able to do like: Levels.getlevelCount;

So I figured out that I could do a 3 dimensional Array: int [][][] AllLevels = new int [][][]

But my question is: Are there anyway to declare an array within another sort of like this:

int[][][] all = new int[][][]{


      int [][] Level01= new int[][] {
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};

int [][] Level02= new int[][] {
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 
    { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};


        };

Above code gives a Compiling Error.

3
  • If you have this kind of arrays, consider removing the data from the code and initializing the arrays from file/db. The people that will have to mantain your code will thank you. Commented Jan 21, 2012 at 22:47
  • I dont have any files. My tile editor gives me this. And I dont see how like my graphic designer will care :D Commented Jan 21, 2012 at 22:50
  • 1
    You already got the answer, I'd just like to point you to oracle.com/technetwork/java/codeconv-138413.html Java Naming Conventions Commented Jan 21, 2012 at 22:55

3 Answers 3

2

You can't do exactly what you have there but you can do this.

int [][] twod1 = {{1, 1,}, {-1, -1}};

int [][] twod2 = {{1, 1,}, {-1, -1}};

int [][][] threed = {twod1, twod2};

Or you can do it in reverse if you prefer.

int [][][] threed2 = {{{1, 1,}, {-1, -1}},
                     {{1, 1,}, {-1, -1}}};

int [][] twod3 = threed2[0];

int [][] twod4 = threed2[1];

You can also do it inline, I don't think its very readable though. If you do it inline you also can't use short hand initialization you actually have to declare the new int[][] for each 2-dimensional int array.

int [][] twod5; 

int [][] twod6;

int [][][] threed3 = { twod5 = new int[][]{{1, 1,}, {-1, -1}},
                       twod6 = new int[][]{ {1, 1,}, {-1, -1}}};
Sign up to request clarification or add additional context in comments.

5 Comments

Great! But are they're anyone to replace the {twod1, twod2}; with actually initialize?
@AndroidXtr3meN I updated my answer with the reverse scenario is that closer to what you need?
This all compiles so you should be able to copy/paste these segments if you need.
Thank you, but int [][][] threed3 = { int [][] array2d= new int[][]{{1, 1,}, {-1, -1}}; }; didnt work somehow
change it to int [][] array2d; int [][][] threed3 = { array2d= new int[][]{{1, 1,}, {-1, -1}} }; You cannot declare an array inside another array, only assign it.
2
int[][][] all = .....

int[][] level1 = all[0];
int[][] level2 = all[1];

Comments

2

Of course, here's a 2D example:

int[] level1, level2;
int[][] all = new int[][] {
    level1 = new int[]{ 1, 2 },
    level2 = new int[]{ 3, 4 }
 };

So first we declare the variables for each level level1 and level2. I'm assuming you want references to them judging from your example. If not, then you can leave them out. So a multi-dimensional array is just an array of arrays of arrays... So you can simply initialize each element of the top-level array separately.

Then notice that the assignment operator = actually returns a value, the value of the assignment.

7 Comments

Well I meant Declare a 2 Dimensional array in a 3rd. Another word one step more than you show
The idea is the same. I just did a 2D example to illustrate the concept used.
You need to declare level1 and level2 separately, as in tskuzzy's example, and then assign them in the declaration of all -- if you want to use this approach.
@AndroidXTr3meN: Declare Level01 and Level02 outside of the first array. Then leave out the int[][] declarations in the array initializer.
But if i have lets say 30 arrays then i have to do like int [][][] = and all the arrays...takes forever
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.