0

I'm running into problems trying to assign a 2d array to a 3d array, so I thought i'd ask a question about 3d and 2d arrays.

Say I have a masterArray[][][] and wanted to put childArray1[][] and childArray2[][] into it. This is how I have done it and was wondering if that is the correct way of applying it:

private int[][][] masterArray;
private int[][] childArray1 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 1, 1, 0, 1},
        {1, 0, 1, 1, 0, 1, 8, 1, 0, 1},
        {1, 0, 7, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 0, 1, 0, 1},
        {1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
        {1, 0, 0, 0, 0, 0, 0, 9, 0, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};
private int[][] childArray2 = {
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
        {1, 1, 1, 1, 7, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 0, 0, 0, 1, 1, 1},
        {1, 1, 1, 9, 1, 1, 8, 0, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

Ok, so in my init method I use these some methods to set the child arrays into the master array. What I was curious about was how this exactly works. I assumed the following:

    masterLevel = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
            masterArray[currentLevel][x][y] = childArray1[x][y];
        }
    }

Would that work? In my application things aren't working so I picking out code that I am not 100% sure on.

3 Answers 3

1

It doesn't really matter how you organize a 3d array as long as you put things in the same way as you take them out.

From your comment on another answer it seems that you are having problem with element order ([currentLevel][x][y] = childArray[y][x];)

It seems you mixed MAP_HEIGHT and MAP_WIDTH. It should be:

masterLevel = new int[MAX_LEVELS][MAP_HEIGHT][MAP_WIDTH];

then you can use:

master[currentLevel][x][y] = childArray[x][y];
Sign up to request clarification or add additional context in comments.

1 Comment

I see... I always assumed that it would read [columns][rows].
1

In Java multi-d arrays are actually arrays of arrays. So they can even be disjoint. In the code you posted you refer to a variable called currentLevel that you did not define. I am sure that is defined in some code you did not post. Also don't forget that arrays are zero index. This code should work.

masterArray = new int[MAX_LEVELS][MAP_WIDTH][MAP_HEIGHT];
for (int currentLevel = 0; currentLevel < MAX_LEVELS; currentLevel++) {
    for (int x = 0; x < MAP_WIDTH; x++) {
        for (int y = 0; y < MAP_HEIGHT; y++) {
                masterArray[currentLevel][x][y] = childArray1[x][y];
            }
        }
    }

If you ever work with massive arrays and need speed then you could look at System.arrayCopy();

1 Comment

currenLevel is defined but when assigning the child array to master array, I didn't want to change the currentLevel. Apparently I got it to work, but I had to switch the variables around, which still confuses me. ex: [currentLevel][x][y] = childArray[y][x];
0
    String[]        arr1D;
    String[][]      arr2D; 
    String[][][]    arr3D; 

    arr1D = new String[] { "1", "2", "3" };

    //////////////////////////////////////////////////////////////////////////
    //assign 1D array to element of 2D array
    ////////////////////////////////////////////////////////////////////////// 
    arr2D = new String[][] {  
                                arr1D ,
                                arr1D ,
                                arr1D 
                           }; 
     /*
     //  OR
     arr2D = new String[3][];
        arr2D[0] = arr1D;
        arr2D[1] = arr1D;
        arr2D[2] = arr1D;       
    //  OR 
     arr2D = new String[][] {  
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } ,
        new String[] { "1", "2", "3" } 
                };      

    */
    //////////////////////////////////////////////////////////////////////////
    //assign 2D array to element of 3D array
    //////////////////////////////////////////////////////////////////////////

    arr3D = new String[][][] {  
            arr2D ,
            arr2D ,
            arr2D 
          }; 
    /*
    // OR   
        arr3D = new String[3][][]; 
        arr3D[0] = arr2D;
        arr3D[1] = arr2D;
        arr3D[2] = arr2D; 
    */  

Comments

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.