An Introduction to Multi-Dimensional Arrays In C and Java
What are multi-dimensional arrays?
Arrays are the most fundamental data structure, to store and manage a collection of elements of the same data type. Extending to this concept we have Multi-Dimensional Arrays which allow us to organize data in a grid-like structure.
In simple words, a multi-dimensional array can be considered as arrays of arrays. Here we will learn in-depth how multi-dimensional arrays work under the hood in languages like C and how Java implements it differently.
When to use
Just like linear Arrays, when you need to store several values of the same data type but want to access them using row and column indices / tabular format.
The several dimensions can also have different significance of their own, for example - Storing information of the 2D coordinate system in a 2D Array can mean one index represents the steps in the x-axis while the other dimension stores steps in the y-axis.
Other good examples could be Matrices in Mathematics, Dynamic Programming, 3D scenes in Computer Graphics, Tensors in Neural Networks and Machine Learning, and many more.
Declaration of Multi-Dimensional Arrays
In general, an n-dimensional array declaration looks like this:
<data type> array_name [size1][size2]…..[size n];
For example,
int arr[2][3][4]
Three Dimensional Array.int arr[2][3][4][5]
Four Dimensional Array.Here size1, size2 . . . sizen , represent the size of the nth dimension array.
Memory Allocation and Addressing
Arrays are contiguous i.e. the elements of an array are stored in consecutive memory locations. Each element in the array is stored right after the previous element in memory.
When we declare a Multi-dimensional array for example :
<data type> array_name [size1][size2]…..[size n];
It can store up to size1 x size2 x . . . x sizen elements of the declared data type and allocates memory equal tosizeof<data type> * Number of elements
bytes.While the concept behind Multi-Dimensional Arrays remains the same across all languages, the way they work in C and Java inside the memory are slightly different.
Working in C
C language likes to keep things simple and treats Multi-dimensional arrays as a linear array. Generally, languages do that in two ways, row-major or column-major form. C uses row-major form i.e. consecutive elements of a row are next to each other. Again not making anything complicated C follows 0-based indexing.
Memory Access in 1-D arrays is usually done like following :
arr[4] = 32;
Let's break it down...
arr
is a pointer to the memory location of the beginning of the allocated memory for the array.
[4]
represents the offset from the starting address.So, the final address is :
arr + sizeof<data type>*offset
Note: While adding something to a pointer, C implicitly multiplies the size of the data type according to the data type of the pointer.
Memory Access in 2-D arrays is just an extension of 1-D addressing as we discussed above. The memory location of the entire 2D array is determined by the address of the first byte of memory. So let's access a general location of a 2D array of m rows and n columns, say,
arr[i][j]
arr
is a pointer to the first byte, so to reach the ith row we need to addi * n <Number of columns>
Now we are in the ith row. To move to the jth column is easy now, we just add
j
to the address.The final address looks like:
arr + i*n + j
Extending this to 3-D Arrays of say size
m x n x p
we will havearr[i][j][k]
mapped toarr +( (i * n * p) + (j * p) + k )
We can generalize the formula for Arrays of m-dimensions,
Consider an m-dimensional Array : arr [u0][u1] . . . . [um-1]
The position of arr [i0][i1] . . . . [im-1] is
Implementation in Java
In Java, Multi-Dimensional Array elements are not stored contiguously but are stored as arrays of arrays. This means that a two-dimensional array is essentially an array of arrays where each element of the outer array is itself an array representing a row of the matrix. This concept can be extended to arrays of higher dimensions as well.
Source: https://www.knowprogram.com/java/multidimensional-array-java/
This implies that every row can be at a different memory location. This gives birth to a very interesting concept of jagged arrays.
In this article, we learned about Multi-Dimensional Arrays and how useful they are.
Subscribe to my newsletter
Read articles from Puneet Pradhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by