0

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

2
  • Why do you want the third parameter? Can't you just use the new int[LevelGridHeight][LevelGridWidth] Commented Apr 6, 2015 at 17:11
  • For specific information about each area of the level Commented Apr 6, 2015 at 17:19

3 Answers 3

2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Kevin, could you possibily give me an example of what creating the a subarray and adding it into the array? Also why are you not declaring the size of your array?
Thankyou kevin, I just want to use the 3d array to store a value or two that represent properties about each value in the 2d array. You think this will drastically slow my program down? I will essentially use it just like a 2d array but will occasionally edit the 2 or 3 property values
@Espore I really wouldn't be worried about speed. There won't be a huge difference between any of the approaches that have been mentioned. What you need to worry about is your own sanity- in other words, how easy it is to maintain your program. Big arrays like this are harder to maintain than Objects.
1
int[][][] arr3d = new int[][][]{{{1 , 2} , {4 , 5} , {{1 , 3}}};

//or

int[][][] arr3d = new int[x][y][z];
int[2][1][4] = 5;

But you should really look for another way to represent data, if possible. The size of memory required to store a 3d-array gets pretty big in no time.

3 Comments

How would you suggest I represent the data more efficiently? Thankyou for the answer! :)
well, thats not that easy to answer, since i don't know what data you store and how you use it. For example, load the areas dynamically instead of all at once
I can setup so that areas are loaded dynamically I think. It's just everything currently needs to be on screen and so I can't increase the efficiency like that currently
0

With indexes:

        int[][][] start_Grid = {
            { { /*[0][0][0] */1, /*[0][0][1] */ 2, /*[0][0][2] */ 3, }, {/*[0][1][0] */ 1, /*[0][1][1] */ 2, /*[0][1][2] */ 3 } },
            { { /*[1][0][0] */1, /*[1][0][1] */56, /*[1][0][2] */ 55 }, { /*[1][1][0] */1, /*[1][1][1] */56, /*[1][1][2] */55 } }

    }

2 Comments

Could you please give me a little more information as to how indexes are helpful?
I just put the indexes of the array values in comment to clarify their position in the array and to be more clear for you

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.