Arrays are used to store data and information on various data types. The greatest advantage of arrays is that they allow all data to be accessed in O(1) time complexity.
3-D arrays are referred to as multi-dimensional arrays. Multi-dimensional arrays are defined as an “array of arrays” that store data in a tabular form.
Imagine this, an array list of data elements makes a 1-D (one-dimensional) array. An array of 1-D arrays makes a 2-D (two-dimensional) array. Similarly, an array of 2-D arrays makes a 3-D ( three-dimensional) array.
A 3-D array can be declared as follows:
int arr[4][5][8]// declares an 3-D integer array
This array can store a total of 4 X 5 X 8 = 160 elements.
Lines 5-9: We defined a 3-D array with the name of arr[] and it would have dimensions of
Line 12: We print the value of the element at the index [0][0][0] of the array arr.
Lines 15-27: This nested loop structure iterates through each element of the 3-dimensional array arr and prints out its indices along with its value.
After understanding 3-D array implementation in C++, it's time to understand that same example in Java.
Click the "Run" button to execute the 3D array example below:
Lines 3-7: We defined a 3-D array with the name of arr[] and we have assigned values to its elements.
Line 9: We print the value of the element at the index [0][0][0] of the array arr.
Lines 13-25: This nested loop structure iterates through each element of the 3-dimensional array arr and prints out its indices along with its value.