Search code examples
javaarraysmultidimensional-arrayprocessing2d-games

How to a populate a 3d array with specific values


I hope this doesn't seem like a simple question. I've been able to populate a 2D array by this method before. But i'm not sure how to use this grid or a similar grid to populate a 3D array. This may be a really simple question but I hope somebody can point me in the right direction. I want to use it to produce a 2d game level.

int[][][] LevelGrid = new int[LevelGridHeight][LevelGridWidth][3]; 

int[][][] start_Grid = { {5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,},
                         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
                         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
                         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
                         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
                         {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,} }

Thankyou


Solution

  • You can think of a one-dimensional array as an array of "things", things being whatever type the array is.

    int[] array1 = {1, 2, 3};
    int[] array2 = {4, 5, 6};
    int[] array3 = {7, 8, 9};
    int x = array1[0];
    

    You can think of a two-dimensional array as an array of arrays.

       int[][] array2d= {array1, array2, array3};
       int[] xArray = array2d[0];
       int x = xArray[0]; //array2d[0][0];
    

    And you can think of a three-dimensional array as an array of arrays of arrays.

       int[][][] array3d = {array2d_1, array2d_2, array2d_3};
       int x = array3d[0][0][0];
    

    So you can either build the sub-arrays first and then add them to the 3D array, or you can do it all in one block:

    int[][][] array3D = { 
    { {0,1,2}, {0,1,2} }, { {0,1,2}, {0,1,2} }, 
    { {0,1,2}, {0,1,2} }, { {0,1,2}, {0,1,2} }
    };
    

    Edit: Here is an example of building the sub-arrays first, and then building the 3d-array from them:

    void setup() {
    
      //one-dimensional arrays are arrays of things
      int[] array1 = {1, 2, 3};
      int[] array2 = {4, 5, 6 };
      int[] array3 = {7, 8, 9};
    
      int[] array4 = {10, 11, 12};
      int[] array5 = {13, 14, 15};
      int[] array6 = {16, 17, 18};
    
      //two-dimensional arrays are arrays of arrays
      int[][] row1 = {array1, array2, array3};
      int[][] row2 = {array4, array5, array6};
    
      //three-dimensional arrays are arrays of arrays of arrays
      int[][][] array3d = {row1, row2};
    }
    

    And I'm not setting the size because I'm using the array initialization shorthand, so I don't need to set the size ahead of time. Notice that you don't set the size when you initialize start_Grid either.

    Also note that like somebody else said, using arrays like this can get pretty unwieldy. So unless you have a good reason for using arrays, you probably should use some kind of data structure. I imagine that whatever your "bottom-level" arrays represent could be represented by an Object of some kind, and then you would only need a 2D array, which is easier to think about. But it's really up to you- a 3D array is just a 2D array of arrays!