Types of Arrays in Data Structures: 2D, 3D, Jagged Arrays

Types of Arrays in Data Structures: 2D, 3D, Jagged Arrays

15 Apr 2024
Beginner
10.4K Views
25 min read
Learn via Video Course & by Doing Hands-on Labs

Data Structures & Algorithms Free Course

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

  1. 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.

two-dimensional array in data structure

Syntax for Declaration of a Two-Dimensional Array

dataType arrayName[size1][size2]...[sizeN];

  1. Three-Dimensional Array or 3D Array

It contains three dimensions, so it can be considered an array of two-dimensional arrays.

multi-dimensional array in data structure

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

  1. 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};

  1. 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

  1. 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    

  1. 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

  1. 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  

  1. 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()
    

public class JaggedArray {
    public static void main(String[] args) {
        // create 3 row arrays having different sizes (no of columns)
        int[] row1 = {1, 2, 10, 15};
        int[] row2 = {5, 6};
        int[] row3 = {7, 20, 9};

        // storing base address of each row array
        int[][] jagged = {row1, row2, row3};

        int[] sizes = {4, 2, 3};

        System.out.println("Elements in matrix form as follows:");
        for (int i = 0; i < 3; i++) {
            // getting current (ith) row
            int[] row = jagged[i];

            for (int j = 0; j < sizes[i]; j++) {
                // for ith row having sizes[i] no. of columns
                System.out.print(row[j] + " ");
            }
            System.out.println();
        }
    }
}
    

#include  
using namespace std; 

int main() 
{ 
	// create 3 row arrays having different sizes 
	// ( no ofcolumns) 
	int row1[] = { 1, 2, 10, 15 }; 
	int row2[] = { 5, 6 }; 
	int row3[] = { 7, 20, 9 }; 

	// storing base address of each row array 
	int* jagged[] = { row1, row2, row3 }; 

	int sizes[] = { 4, 2, 3 }; 

	cout << "elements in matrix form as follow" << endl; 
	for (int i = 0; i < 3; i++) { 

		// getting current(ith) row 
		int* ptr = jagged[i]; 

		for (int j = 0; j < sizes[i]; j++) { 
			// for ith row having sizes[i] no. of 
			// columns 

			cout << *(ptr + j) << " "; 
			// *ptr have base address 
			// adding j means access jth 
			// element for particular(ith) row 
		} 
		cout << endl; 
	} 

	return 0; 
}
    
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.

FAQs

Q1. What are the different types of arrays?

One-dimensional, multi-dimensional, and jagged arrays are some of the different types of arrays.

Q2. What is a 3-dimensional array in data structures?

An array having three dimensions that form a cube of elements is referred to as a 3-dimensional array in data structures.

Q3. What are the two types of multidimensional arrays?

Rectangular arrays & jagged arrays are the two varieties of multidimensional arrays.

Q4. What is a multidimensional array?

An array with more than one dimension, such as a matrix or cube, is referred to as a multidimensional array.
Share Article
Batches Schedule
About Author
Amit Kumar Ghosh (SDE and Mentor)

A passionate professional with over 6 years of experience in development and training. He is passionate about learning new technologies and sharing his experience with professionals. He is an expert in C/C++, Java, Python and DSA.
Self-paced Membership
  • 22+ Video Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Accept cookies & close this