The dimension of an array can simply be defined as the number of subscripts or indices required to specify a particular element of the array. Dimension has its own meaning in the real world too and the dimension of an array can be associated with it like:-
1-dimension array can be viewed as 1-axis i.e., a line.
Analogy:
Let’s understand the dimensionality of an array by an analogy of a library. In Library, Let’s consider books as individual elements. Books are kept on the shelves of the racks in the library where each rack and shelf are indexed. Here, a single shelf can be viewed as a 1-D (1-Dimensional) array of books, then a single rack with several shelves can be considered to be a 2-D (2-Dimensional) array and the complete library with several racks can be viewed as a 3-D (3-Dimensional) array. And we require rack number, shelf number, and position of the book on the shelf to get a particular book from the library. Similarly, an institution can have several libraries on its campus and thus the institution can be viewed as a 4-D (4-Dimensional) array with individual libraries as its elements.
1-D array:
1-D array or 1-Dimensional array requires only one subscript to access the individual element as arr[x], where arr is the array and x is the subscript or the linear index. In real world it can be associated with a line that has only one axis. We can understand a 1-D array as a line holding some value in each integral position.
For example:
Lets consider: arr = {1, 2, 3, 4}
Here, a[0] = 1, a[1] = 2Note: 1 subscript is used to access the element.
2-D array:
A 2-D array or 2-Dimensional array requires two subscripts to access the individual element as arr[x][y] or arr[x,y], where arr is the array and x and y are the subscripts. In the real world, it can be associated with a plane with two axes, i.e., the x-axis and the y-axis. Mathematically, it can also be viewed as M*N matrix.
For example:
Lets consider, arr = {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}
Here, a[0][0] = 1, a[1][2] = 3Note: 2 subscripts are used to access the element.
3-D array:
3-D array or 3-Dimensional arrays requires three subscripts to access the individual element as arr[x][y][z] or arr[x,y,z], where arr is the array and x, y and z are the subscripts. In the real world, it can be associated with space which has three axes i.e., x-axis, y-axis, and z-axis corresponding to length, breadth, and height.
For example:
Lets consider, arr = {{{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4},
{1, 2, 3, 4}}, {{1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}, {1, 2, 3, 4}}
Here, a[0][0][1] = 2, a[1][2][3] = 4Note: 3 subscripts are used to access the element.
N-D array:
Although the dimensions greater than 3 cannot be viewed in the real world, we can represent N-D (N-Dimensional) array as arr[x1][x2][x3]…..[xn] or arr[x1, x2, x3,…., xn], where arr is the array and x1,x2,x3…., xn are the subscripts. Declaration of an array should also be done keeping dimensions in mind.