array inside array unity code example

Example: how to make an array of arrays unity

//creates an array of arrays int[][] myarray;  //creates an array which contains 20 arrays the arrays are all empty and unitialized, they will need to be initialized, there is currently just 20 starting points reserved in memory. They point to nothing really. myarrays = new int[20][];  //the first array is size 3 with the element 0 & 1 & 1 myarrays[0] = {0,1,1};  myarrays[0][0] = ? //based on the statement above it equals 0 myarrays[0][1] = ? //based on the statement above it equals 1 myarrays[0][2] = ? //based on the statement above it equals 1 as well   //the following code will take every array in myarrays and make them equal to arrays of size 10, the arrays are empty but now they point to something, its probably zero by default due to internal code. for (int x = 0; x <= myarrays.size(); x++) {     myarrays[x] = new int[10]; } //technically the code above still hasnt set what the value of each element in the arrays are, it simply now says its size is definitely 10, internally the code has probably set those memory values to zero however to help prevent unexpected errors, that's not guaranteed I don't think however and it would probably be best to set each value to be certain different machines and things dont do things differently and create unexpected and hard to find errors.   //the follow code will create an array that contains 20 arrays, each of those arrays are of size 10, and each of them will have the value set to 0;  int[][] myarrays = new int[20][];  //careful on this for statement, it is less than not less than or equal too, this has to do with size starting to count from 1 and x starting to count from 0. You must start from 0 because the first index in an array is 0.  for(int x = 0; x < myarrays.size(); x++) {       myarrays[x] = new int[10];    for (int y = 0; y < myarrays[x].size(); y++)     {        myarrays[x][y] = 0;     } }  //Do note that i used the size function to get the size, thats because you shouldn't used numbers like 20 or 10 even if thats what you think you want. You should declare a constant called NUMBER_OF_ARRAYS and NUMBER_OF_ELEMENTS_IN_ARRAYS or something like that and substitute those that way you can change the size of the arrays with a few keyboard strokes.

Tags:

Misc Example