Explanation of Two-Dimensional Array in Data Structure
Explanation of Two-Dimensional Array in Data Structure
In this tutorial, we will discuss “Explanation of Two-Dimensional Array in Data Structure.”

Structure of a 2D array
How it’s defined in programming
In many programming languages, a 2D array is defined as an array of arrays. This means each row is itself an array
Example
Consider a 2D array with 3 rows and 4 columns(often written as a 3X4 matrix).
array_2D=[
[1,2,3],
[4,5,6],
[7,8,9]
]
Here, array_2D[0][0] accesses the element in the first row and first column, which is 1
in this array
The first row is [1, 2, 3].
The second row is [5, 6, 7].
Each column contains the values from each row at the same index
Accessing elements
To access any elements, you specify its row and column.
For instance, in 3X3 array, array[1][2] would mean the element in the second rows and third column
This indexing usually starts from 0, so array[0][0]
points to the first row, first column.
Visual representation
If you visualize a 2D array as a grid, each row is like a horizontal line, and each column is a vertical line.
For example
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
Here:
array[0][0]=1
array[1][1]=5
array[2][2]=9
Memory Layout
In memory, a 2D array is typically stored in one of two ways: row-major or column-major order
Row-major order : -All elements in the first row are stored first, followed by all elements in the second row, and so on.
column-major order- All elements in the first column are stored first, followed by all elements in the second column, and so on.
For example, in row-major order for the array above, the memory layout would be
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Applications of 2D Arrays
Matrix operations: In matrix, 2D arrays are used to represent matrix and perform matrix operations.
Grid and maps: Games and simulations often use 2d arrays to represent grids or maps
Storing tabular data: Like an excel sheet, 2D arrays store rows of records
Common Operations
Traversing: Looping through each elements in the 2D array, either row by row or column by column.
Row or Column access: Accessing a specific row or column directly
Updating elements: Assigning new values to specific positions
For example, to print all elements row by row:
for row in array_2d:
for element in row:
print(element, end=” “)
print()
Limitations and Advantages
Limitations
Fixed size:In programming languages with static arrays m the size of 2D array is fixed upon initialization
Memory intensive – If the array is sparsely populated, it wastes memory
Advantages
Organized data storage – Allow you to handle data in tubular format easily
Efficient Access –
allow direct access to element using indices
Summary (Explanation of Two-Dimensional Array in Data Structure)
A 2D array is a structured way to store data in a grid of rows and columns, making it a powerful tool for many applications that involve tabular data. Understanding indexing memory representation and common operations can help
Examples for 2D array
Python
# Example for one dimensional array in Python
#Creating a 2D array
array=[
[1,2,3],
[4,5,6],
[7,8,9]
]
#Accessing an elements
print("print first element")
print(array[0][0])
print("print 6th element")
print(array[1][2])
When the above code is executed, it produces the following result
print first element 1 print 6th element 6
Java
// Example for one dimensional array in Java
//Creating a 2D array
class ArrayEx{
public static void main(String[] args) {
int[][] array={
{2,4,6},
{8,10,12},
{14,16,18},
};
//Accessing an elements
System.out.println("print first element");
System.out.println(array[0][0]);
System.out.println("print 5th element");
System.out.println(array[1][1]);
}
}
When the above code is executed, it produces the following result
print first element 2 print 5th element 10
C++
// Example for two dim array in C++
#include <iostream>
using namespace std;
int main() {
int array[3][3]={
{3,6,9},
{12,15,18},
{21,24,27}
};
cout<<"first element\n";
cout<<array[0][0];
cout<<"\nlast element\n";
cout<<array[2][2];
return 0;
}
When the above code is executed, it produces the following result
first element 3 last element 27
C
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int array[3][3]={
{3,6,9},
{12,15,18},
{21,24,27}
};
// iterate through the rows
for(int i=0; i<3; i++){
// iterate through the columns
for(int j=0; j<3; j++){
printf("%d ",array[i][j]);
}
printf("\n ");
}
return 0;
}
When the above code is executed, it produces the following result
3 6 9 12 15 18 21 24 27
PHP
<?php
// Example program for 2D array in Python
$array=[
[2,4,6],
[8,10,12],
[14,16,18]
];
foreach($array as $row){
foreach($row as $value){
echo $value. ' ';
}
echo "\n";
}
?>
When the above code is executed, it produces the following result
2 4 6 8 10 12 14 16 18
Swift
// Example program to 2D array
let arr:[[Int]]=[
[1,2,3],
[4,5,6]
]
//Display the 2D array
for row in arr{
for element in row{
print(element, terminator: " ")
}
print()
}
When the above code is executed, it produces the following result
1 2 3
4 5 6
Ruby
#Example program to 2D array
arr=[
[1,2,3],
[4,5,6]
]
#Display the 2D array
arr.each do |row|
row.each {| element| print "#{element}"}
puts
end
When the above code is executed, it produces the following result
123 456
Java script
// Example program to 2D array
let arr=[
[1,2,3],
[4,5,6]
];
//Display the 2D array
for (let i=0; i<arr.length; i++){
for (let j=0; j<arr[i].length; j++){
process.stdout.write(arr[i][j] + ' ');
}
console.log();
}
When the above code is executed, it produces the following result
1 2 3 4 5 6
Suggested for you
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C
Similar post
One dimensional array in C language
One dimensional array in C++ language
Two dimension array in Java language
Two dimension array in C language
Two dimension array in C++ language
Three dimension array in Java language
Three dimension array in C language
Three dimension array in C++ language