Nest one Array within Another Array.
I am sure that there will be a practical use for nesting arrays further down the line.
But.
Just wanna put the question out there and see what comes back.
So have at it.
cheers.
Nest one Array within Another Array.
I am sure that there will be a practical use for nesting arrays further down the line.
But.
Just wanna put the question out there and see what comes back.
So have at it.
cheers.
How about a table of rows that are not the same length?
You can often use objects instead or flatten them, but it can be useful as-is.
Imagine you have a web app that stores user information in an array, it might follow the pattern of:
[ [username, picture, email], [username, picture, email] ... ]
At some point you just want a list of email addresses, which you know you can access in a loop using something like array[i][2]
In real life you’d be more likely to use JSON for the example above, but it illustrates one simple use case.
There’s data structures in which a master array that contains variable length arrays are very helpful such as Hash Tables.
When I was in school, I once had to write a text editor in C and the general strategy was to create an array which represented a page of text. Each location in the array would reference another array which was a specific line of text (of variable length). This was useful because I could have one master structure that represented the entire page and allow me to index into a line, as well as a sub structures to represent those lines and let me index to get specific chars. This was useful in updating cursor position, for example. Basically, an array of arrays was a very nice representation for this application.