What is a 3-D array?

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

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.

An illustration to understand concept of 3D array
An illustration to understand concept of 3D array

Coding examples

Now we understand what a 3-D array means, it is time for us to understand how we could program it, and for that purpose, we will be using C++ and Java programming languages.

3-D array in C++

Click the "Run" button to execute the below example.

#include <iostream>
using namespace std;
int main() {
int arr[2][3][3] =
{
{ {0,1,2}, {2,3,4}, {6,7,1} },
{ {6,7, 1}, {8,9, 2}, {9,14, 22} }
};
// accessing a single value i.e 0
cout << arr[0][0][0] << endl;
// output each element's value by iterating through the array
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 3; ++k)
{
cout << "Element at arr[" << i << "][" << j
<< "][" << k << "] = " << arr[i][j][k]
<< endl;
}
}
}
return 0;
}
  • Lines 5-9: We defined a 3-D array with the name of arr[] and it would have dimensions of 2×3×32\times3\times3 and we have assigned values to its elements.

  • 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.

3-D array in Java

Click the "Run" button to execute the 3D array example below:

class HelloWorld {
public static void main( String args[] ) {
int[][][] arr = {
{ {0,1,2}, {2,3,4}, {6,7,1} },
{ {6,7, 1}, {8,9, 2}, {9,14, 22} }
};
// accessing single value i.e 0
System.out.println(arr[0][0][0]);
// output each element's value by iterating through the array
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 3; ++k)
{
System.out.println("Element at arr[" + i + "][" + j
+ "][" + k + "] = " + arr[i][j][k]);
}
}
}
}
}
  • 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.

Copyright ©2024 Educative, Inc. All rights reserved