09
NovTypes of Arrays in Data Structures: 2D, 3D, Jagged Arrays
Types of Arrays in Data Structures: An Overview
Types of Arrays in Data Structures refer to 2D and 3D arrays.In this DSA tutorial, we will learn multidimensional arrays in data structures. To further enhance your understanding and application of multidimensional array concepts, consider enrolling in the Best Data Structures and Algorithms Course, to gain comprehensive insights into effective data structure utilization for improved problem-solving and time management.
What are Multi-dimensional Arrays in Data Structures?
A multi-dimensional array is an array with more than one dimension. It can be thought of as an array of arrays.
In this tutorial, we will learn about 2D and 3D arrays
- Two-Dimensional Array or 2D Array
A two-dimensionalarray is also known as a matrix, which is an array of arrays. It consists of a grid of elements that can be accessed using two indices.
Syntax for Declaration of a Two-Dimensional Array
dataType arrayName[size1][size2]...[sizeN];
- Three-Dimensional Array or 3D Array
It contains three dimensions, so it can be considered an array of two-dimensional arrays.
Syntax for Declaration of a Three-Dimensional Array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension];
Read More - DSA Interview Questions and Answers
Initialization of a Multi-dimensional Array in Data Structures
- Two-Dimensional Array or 2D Array
int arr[2][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[][3] = {{1, 3, 0}, {-1, 5, 9}};
int arr[2][3] = {1, 3, 0, -1, 5, 9};
- Three-Dimensional Array or 3D Array
int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};
Accessing elements of a Multi-dimensional Array in Data Structures
- Two-Dimensional Array or 2D Array
arr = [[5, 6, 7], [3, 6, 8]]
print(arr[0][2])
public class Main {
public static void main(String[] args) {
int[][] arr = {{5, 6, 7}, {3, 6, 8}};
System.out.println(arr[0][2]);
}
}
#include <iostream>
using namespace std;
int main() {
int arr[2][3] = {{5, 6, 7}, {3, 6, 8}};
cout << arr[0][2];
return 0;
}
Output
7
- Three-Dimensional Array or 3D Array
arr = [
[
[3, 4, 2, 3],
[0, -3, 9, 11],
[23, 12, 23, 2]
],
[
[13, 4, 56, 3],
[5, 9, 3, 5],
[3, 1, 4, 9]
]
]
print(arr[0][2][3])
public class Main {
public static void main(String[] args) {
int[][][] arr = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};
System.out.println(arr[0][2][3]);
}
}
#include <iostream>
using namespace std;
int main() {
int arr[2][3][4] = {
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}
};
cout << arr[0][2][3];
return 0;
}
Output
2
Traversing Multidimensional Arrays in Data Structures
- Two-Dimensional Arrays or 2D Arrays
fruits_2d = [
["Apple", "Mango", "Banana"],
["Orange", "Grapes"]
]
for row in fruits_2d:
for fruit in row:
print(fruit)
public class Main {
public static void main(String[] args) {
String[][] fruits2D = {
{"Apple", "Mango", "Banana"},
{"Orange", "Grapes"}
};
for (String[] row : fruits2D) {
for (String fruit : row) {
System.out.println(fruit);
}
}
}
}
#include <iostream>
#include <string>
int main() {
// 2D array of strings representing fruits
std::string fruits_2d[][3] = {
{"Apple", "Mango", "Banana"},
{"Orange", "Grapes"}
};
// Iterate through each row and column
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
std::cout << fruits_2d[i][j] << '\n';
}
}
return 0;
}
Output
Apple
Mango
Banana
Orange
Grapes
- Three-Dimensional Arrays or 3D Arrays
fruits_3d = [
[["Apple", "Mango"], ["Banana", "Orange"]],
[["Grapes", "Kiwi"], ["Pineapple", "Strawberry"]]
]
for matrix in fruits_3d:
for row in matrix:
for fruit in row:
print(fruit)
public class Main {
public static void main(String[] args) {
String[][][] fruits_3d = {
{{"Apple", "Mango"}, {"Banana", "Orange"}},
{{"Grapes", "Kiwi"}, {"Pineapple", "Strawberry"}}
};
for (String[][] matrix : fruits_3d) {
for (String[] row : matrix) {
for (String fruit : row) {
System.out.println(fruit);
}
}
}
}
}
#include <iostream>
#include <string>
int main() {
// 3D array of strings representing fruits
std::string fruits_3d[][2][2] = {
{{"Apple", "Mango"}, {"Banana", "Orange"}},
{{"Grapes", "Kiwi"}, {"Pineapple", "Strawberry"}}
};
// Iterate through each matrix, row, and column
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
std::cout << fruits_3d[i][j][k] << '\n';
}
}
}
return 0;
}
Output
Apple
Mango
Banana
Orange
Grapes
Kiwi
Pineapple
Strawberry
Jagged Arrays
It is an array of arrays, where each sub-array can have a different number of elements.
Syntax for Declaration of a Jagged Array
data_type array_name[][] = new data_type[n][]; //n: no. of rows
array_name[] = new data_type[n1]; //n1= no. of colmuns in row-1
array_name[] = new data_type[n2]; //n2= no. of colmuns in row-2
array_name[] = new data_type[n3]; //n3= no. of colmuns in row-3
How to Initialize a Jagged Array in Data Structures?
int[][] arr_name = {
{15, 7, 22},
{67, 81},
{12, 91, 1, 17}
};
Implementation of Jagged Arrays in Different Languages in Data Structures
row1 = [1, 2, 10, 15]
row2 = [5, 6]
row3 = [7, 20, 9]
# storing base address of each row array
jagged = [row1, row2, row3]
sizes = [4, 2, 3]
print("Elements in matrix form as follows:")
for i in range(3):
# getting current (ith) row
row = jagged[i]
for j in range(sizes[i]):
# for ith row having sizes[i] no. of columns
print(row[j], end=" ")
print()
>>> Read More:- Differences Between Array and Linked List |
Summary
This was all about types of arrays in data structures. With this, the topic of arrays in data structures gets completed. Now, the only thing is to implement whatever learned till now. You must know where and what kind of array is required. If you're interested in more tips and guidance, you may also consider our Dsa Training, which can validate your skills and enhance your credibility in the field.