Difference between multidimensional array in C++ and Java
Last Updated :
07 Feb, 2022
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();
}
}
}
OutputPrinting 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
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read