Search code examples
javaandroidarraysmultidimensional-arrayorganization

Initialize Array in an array


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.


Solution

  • 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}}};