A multidimensional array consists of arrays within arrays.
Multidimensional arrays prove useful when organizing data in a tabular format, resembling a table comprising rows and columns.
You can form a two-dimensional array by enclosing each array within its own set of curly braces:
|
Now, myNumbers consists of an array containing two arrays as its elements.
To retrieve elements from the myNumbers array, you need to provide two indexes: one for the array and another for the element within that array. In this instance, the example accesses the third element (2) in the second array (1) of myNumbers.
|
Keep in mind that array indexes commence from 0: [0] represents the first element, [1] denotes the second element, and so forth. |
You also have the capability to modify the value of an element:
|
You can utilize a nested for loop to iterate through the elements of a two-dimensional array, requiring you to reference the two indexes as before.
|
Alternatively, you can employ a for-each loop, which is frequently simpler to comprehend and implement:
|