Open In App

Difference between multidimensional array in C++ and Java

Last Updated : 07 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Multidimensional array in C++, Multidimensional array in Java

Multidimensional Arrays:

Multidimensional arrays are a tabular representation of arrays to store multiple elements. These dimensions can be 1D arrays, 2D-arrays, etc. Multidimensional arrays are available in both C++ and Java, but their implementation and some properties are different.

Implementation in C/C++ :

In C++ a multidimensional array is created internally as a giant linear array. C++ syntax abstracts this linear block of memory into a 2 or 3-dimensional behavior making it easy for the programmer.

Examples:

A 2D array of dimensions 2 rows x 3 cols {{9, 45, 51}, {5, 25, 6}} is implemented as follows (Assuming Integer takes 4 bytes):

Hence, the inner formula for inner element at particular index is given as:

arr[rowIndex][colIndex] = arr + (rowIndex * noOfCols * sizeOfDataType) + coLIndex * sizeOfDataType

Let assume base address to be 3000. Then arr[1][1] = 3000 + (1 * 3 * 4) + 1 * 4 = 3000 + 12 + 4 = 3016.

Because of such implementation, the number of columns must be equal for each row, and it is mandatory to specify column size while declaration in order to access elements properly.

Below is the implementation of the multidimensional array in C++:

C++




// C++ program for multidimension array
// implementation
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Create a 2d integer array,
    // dimensions: 3rows X 5cols
    int arr[3][5] = {
        { 23, 56, 34, 52, 63 },
        { 40, 20, 96, 43, 97 },
        { 75, 51, 10, 82, 43 }
    };
 
    // Traversing of 2D array
    cout << "Printing entire 2d array: "
         << endl;
 
    // Iterate over the rows
    for (int i = 0; i < 3; i++) {
 
        // Iterate over the cols
        for (int j = 0; j < 5; j++) {
 
            cout << "arr[" << i << "][" << j
                 << "]:" << arr[i][j]
                 << "      ";
        }
        cout << endl;
    }
 
    return 0;
}


Output: 

Printing entire 2d array: 
arr[0][0]:23      arr[0][1]:56      arr[0][2]:34      arr[0][3]:52      arr[0][4]:63      
arr[1][0]:40      arr[1][1]:20      arr[1][2]:96      arr[1][3]:43      arr[1][4]:97      
arr[2][0]:75      arr[2][1]:51      arr[2][2]:10      arr[2][3]:82      arr[2][4]:43

 

 Implementation in Java:

In Java, a multidimensional array is implemented as an array of arrays where each index of the base array refers to an entirely different array. So, arr[rowIndex] returns an entire single dimensional array and arr[rowIndex][coLIndex] returns the element at index coLIndex in that single dimensional array.

Examples:

A 2D array of dimensions 3 rows x 5 cols is implemented as follows:

Because of this structure, It is possible to have 2D arrays with different column sizes (even null values) in Java.

Below is the implementation of the multidimensional array in Java: 

Java




// Java program for multidimensional
// array implementation
import java.io.*;
 
class GFG {
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Create a 2D integer array
        // dimensions: 3rows X 5cols
        int[][] arr = {
            { 23, 56, 34, 52, 63 },
            { 40, 20, 96, 43, 97 },
            { 75, 51, 10, 82, 43 }
        };
 
        // Traversing the 2D array
        System.out.println("Printing entire 2d array: ");
 
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
 
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
        System.out.println();
 
        // Reassigning arr[2] to another
        // array
 
        // This is not possible in 2D
        // arrays in C++, instead of
        // there is array of pointers
        arr[2] = new int[] { 82, 53, 64,
                             12, 45, 3 };
 
        // Traversing the array again
        System.out.println(
            "Printing entire 2d array "
            + "after modification: ");
 
        // Iterate over the rows
        for (int i = 0;
             i < arr.length; i++) {
 
            // Iterate over the cols
            for (int j = 0;
                 j < arr[i].length; j++) {
 
                System.out.print(
                    "arr[" + i + "][" + j
                    + "]:" + arr[i][j]
                    + "    ");
            }
            System.out.println();
        }
    }
}


Output

Printing entire 2d array: 
arr[0][0]:23    arr[0][1]:56    arr[0][2]:34    arr[0][3]:52    arr[0][4]:63    
arr[1][0]:40    arr[1][1]:20    arr[1][2]:96    arr[1][3]:43    arr[1][4]:97    
arr[2][0]:75    arr[2][1]:51    arr[2][2]:10    arr[2][3]:82    arr[2][4]:43    

Printing entire 2d array after modification: 
arr[0][0]:23    arr[0][1]:56    arr[0][2]:34    arr[0][3]:52    arr[0][4]:63    
arr[1][0]:40    arr[1][1]:20    arr[1][2]:96    arr[1][3]:43    arr[1][4]:97    
arr[2][0]:82    arr[2][1]:53    arr[2][2]:64    arr[2][3]:12    arr[2][4]:45    arr[2][5]:3    

 
 

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads