Initialize an 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}
                        }

Thus, it prints a 2-dimensional array.

The problem is that I have hundreds of them in the same class and what to organize them in order to be able to do: Levels.getlevelCount;

So, I realized that I can make a three-dimensional array: int [][][] AllLevels = new int [][][]

But my question is: Do I need to declare an array in a different form:

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


        };

The above code gives a compilation error.

+5
source share
3 answers

You cannot do what you have, but you can do it.

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 want.

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

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

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

inline, , . inline, , new int[][] int.

int [][] twod5; 

int [][] twod6;

int [][][] threed3 = { twod5 = new int[][]{{1, 1,}, {-1, -1}},
                       twod6 = new int[][]{ {1, 1,}, {-1, -1}}};
+2
int[][][] all = .....

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

Of course, here is a 2D example:

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

So, first we declare variables for each level level1and level2. I assume that you want to refer to them, judging by your example. If not, you can leave them. Thus, a multidimensional array is an array of arrays of arrays ... Thus, you can simply initialize each element of the top-level array separately.

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

+2
source

All Articles