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
Difference Between Pointers and Array Notations in C++
In C++, pointers and array notations are two ways using which we work with arrays and memory for accessing the data. They have distinct behaviours and are used in different contexts. In this article, we will learn the key differences between pointers and array notations in C++. Difference Between Po
4 min read
Difference Between Array of Characters and std::string in C++
In C++, we have character array and std::string class both of which are used to store the sequence of characters. The character arrays are a part of C-style programming on the other hand std::string is a part of the C++ standard library. In this article, we will discuss what are some major differenc
3 min read
Difference between pair in Multiset and Multimap in C++ STL
Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as âfirstâ and the second element as âsecondâ and the order is fixed (first, second). Pair is used to combine together two values which
5 min read
Difference Between string and char[] Types in C++
In C++, we can store the sequence of characters i.e. string in two ways: either as a std::string object or char array. In this article, we will discuss some major differences between string and char[] in C++.Character Array Type StringsA character array is simply an array in C++ that contains charac
2 min read
Difference between int (*p)[3] and int* p[3]?
Pointers store the address of variables or a memory location. Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format: Syntax: datatype *var_na
2 min read
Difference Between Stack-Allocated and Heap-Allocated Arrays
In C/C++, arrays can be allocated in two areas of memory: the stack and the heap. Each has its own characteristics and use cases. In this article, we will see the key differences between stack-allocated and heap-allocated arrays. Stack-Allocated ArraysThe arrays declared as static arrays in the func
3 min read
Difference between std::set vs std::vector in C++ STL
Vectors: Vectors are containers similar to dynamic arrays, with the ability to resize when a new element is inserted or deleted from it. It is a template of Standard Template Library or STL, which provides more flexibility to the program. Elements of vectors are placed in contiguous storage and are
2 min read
How to Clone a 2D Array With Different Row Sizes in Java?
2D arrays are two-dimensional arrays. These are the simplest forms of multidimensional arrays. Java provides various methods to clone the arrays, but when dealing with 2D arrays having varying sizes, then the process becomes more difficult. Here, we will see different methods to clone 2D arrays with
5 min read
Different ways of accessing array elements in C++
In this article, two unique and different ways of accessing an element from an array rather than the conventional arr[i] expression are discussed below. Using pointer *(arr+1)Using a little manipulation i[arr] for using arrays and the reason behind that. When a C++ program is compiled, a symbol tabl
4 min read
Deep Copy of 2D Array in Java
In Java, performing a deep copy of a 2D array containing objects requires careful consideration to ensure independence between the original and copied arrays. By creating new instances of objects for each element in the array, developers can achieve a true deep copy, preventing unintended side effec
3 min read