Discuss memory representations of 1D and 2D arrays with the help of suitable example
Discuss memory representations of 1D and 2D arrays with the help of suitable examples
One-Dimensional or Single-Dimensional array is considered as the ”list of variables of similar data types”, and each variable can be distinctly accessed by specifying its index in square brackets preceded by the name of that array.
In C++, the declaration of an array variable with the size is enough to allocate space for them in memory. In Java, this is achieved in two steps. First, you must declare a variable of the desired type. Second, you must allocate the memory to hold the array using ‘new’ and assign it to the declared array variable. Hence, arrays are dynamically allocated in Java.
Both C++ and Java support multidimensional array. One of the simplest forms of a multidimensional array is a two-dimensional array or 2-D array. A two-Dimensional array can be expressed as ‘array of arrays’ or ‘array of one-dimensional arrays’.
To declare the two-dimensional array variable, we have to specify the array name followed by two square brackets where the second index is the second set of square brackets.
A two-dimensional array is stored in the form of the row-column matrix, where the first index designates the row and second index shows the column. The second or the rightmost index of an array alters very fastly as compared to the first or left-most index while accessing the elements of an array.
Step by step
Solved in 3 steps with 2 images