Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 584
AndroidXTr3meN Avatar asked Jan 21 '12 22:01

AndroidXTr3meN


People also ask

How do you initialize an entire array with value?

int num[5] = {1, 1, 1, 1, 1}; This will initialize the num array with value 1 at all index. The array will be initialized to 0 in case we provide empty initializer list or just specify 0 in the initializer list. Designated Initializer: This initializer is used when we want to initialize a range with the same value.

Can you initialize variables in an array?

You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. You can either specify the type or allow it to be inferred from the values in the array literal.

How do we initialize an array in C?

Array Initialization Using a Loop An array can also be initialized using a loop. The loop iterates from 0 to (size - 1) for accessing all indices of the array starting from 0. The following syntax uses a “for loop” to initialize the array elements. This is the most common way to initialize an array in C.


1 Answers

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}}};
like image 107
Dev Avatar answered Oct 05 '22 22:10

Dev