1 - Using Arrays of arrays
- These source code examples are on Github
1.1 - Using literal array bracket syntax to create a static array of arrays
So for this example I just have a grid of single digit hex values that is four by four. I start out with just a simple array, and then have four arrays within that array. In each nested array I then have my hex values where the lowest hex value is the first index of the first nested array, things then progress from that index value forward to the end of the nested array, and then continue in the next.
I can then access the primitive value in each nested array, by using the bracket syntax just like a single array, only when doing so I get a nested array rather than a primitive value, so Then need to give the index value of the element that I want in the nested array to get an element with one of my hex values.
If you are still confused maybe it would be best to look at a code example of this.
This is a static form of a 2d array of arrays, however often I will want to have a way to generate such an array, so for this I will want to use some loops, and maybe do so in the body of a function.
1.2 - Using a function that will generate an array of arrays
2 - Using just one linear array
Although arrays of arrays work okay, I often just use a single array, and then work out a formula that is used to figure the proper index value for this single array. Working out an expression for just two dimensions is not so hard, and adding even more levels really is only that much more involved.
2.1 - A static example of using a single array and formula to get the proper index value
So by multiplying the width of the matrix by a y value of an element that I want, and then just adding the x value I can get an element that is at that location. The same trick can then be used to set the values of such elements in such arrays also. In addition looping over the contents of such an array is far more simple. I can do so with just any other array, it is just that I need to use an expression to know what element it is in the matrix.
2.2 - Making a function that will return an API with a single array and get method
Now to do the same thing, only once again make a function that will return my multidimensional array. Only this time it will return the array along with at least a width property. In addition I might also want a height property, and a method that I can use to get a location by way of passing an x and y value.
3 - Three or more dimensions
So then there is working something out for working with three or more dimensions. I quickly put something together for that, but it is still not working the way I would like it to really. I do get around to editing these posts now and then, maybe at some point I will come back to this one, or just come up with a better example all together when it comes to this sort of thing.
4 - Conclusion
For an additional example of a multi denominational array there is my draw points function that I made where I am using an array of arrays to store points as well as settings for a bunch of lies that draw something to a canvas. The typical use case example though is to make some kind of grid module that will be an array fo arrays, or a single array that make use of an expression to knowhow to get and put values into the array. If you are having a hard time making a decision with it though there is always making methods that will convert one from to another, and other that might just be what needs to happen. When working out a project there might be typical forms that are used for various modules, but often developers come up with there own weird formats for things to, present company included.