Back to Course
Arrays

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

Arrays are classified into several types based on their dimensions and structure. The most common types are one-dimensional (1D), two-dimensional (2D), three-dimensional (3D), and jagged arrays (also known as ragged arrays). Each type serves different purposes and is suited for specific use cases.

Overview of Array Types

TypeDescriptionAccessExample
1D ArrayLinear list of elementsarr[i][1, 2, 3, 4, 5]
2D ArrayGrid/table (rows and columns)arr[i][j][[1,2],[3,4]]
3D ArrayCube (layers of 2D arrays)arr[i][j][k][[[1,2],[3,4]],[[5,6],[7,8]]]
Jagged ArrayArray of arrays with varying lengthsarr[i][j][[1,2],[3,4,5],[6]]

Two-Dimensional (2D) Arrays

A 2D array is an array of arrays, forming a matrix with rows and columns. All rows have the same number of columns.

# Python — 2D array
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]
# Access: matrix[row][col]
print(matrix[1][2])  # Output: 6

Key Characteristics

  • Dimensions: m rows × n columns (total elements = m × n).
  • Memory Layout: Row-major (C, C++, Python) or column-major (Fortran, MATLAB).
  • Use Cases: Matrices, images, game boards, spreadsheets.

Three-Dimensional (3D) Arrays

A 3D array is an array of 2D arrays, forming a cube-like structure. It can be visualized as multiple matrices stacked together. Elements are accessed using three indices: arr[x][y][z].

# Python — 3D array
cube = [
    [
        [1, 2],
        [3, 4]
    ],
    [
        [5, 6],
        [7, 8]
    ]
]
# Access: cube[layer][row][col]
print(cube[1][0][1])  # Output: 6

Key Characteristics

  • Dimensions: layers × rows × columns (total elements = l × m × n).
  • Visualization: A stack of 2D matrices.
  • Use Cases: RGB images (3 channels: height × width × color), volumetric data (medical imaging, 3D graphics), time-series data (day × hour × minute).

3D Array in Java

// Java — 3D array
int[][][] cube = new int[2][3][4];

// Access
cube[1][2][3] = 100;

// Initialization
int[][][] cube = {
    {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    },
    {
        {10, 11, 12},
        {13, 14, 15},
        {16, 17, 18}
    }
};

3D Array in C/C++

// C++ — 3D array
int cube[2][3][4] = {
    {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    },
    {
        {13, 14, 15, 16},
        {17, 18, 19, 20},
        {21, 22, 23, 24}
    }
};

// Access
int val = cube[1][2][3];  // Output: 24

Jagged Arrays (Ragged Arrays)

A jagged array (also called a ragged array) is an array of arrays where each inner array can have a different length. This provides flexibility when the number of elements in each row (or column) varies.

Jagged Arrays in Python

In Python, jagged arrays are simply lists of lists with varying lengths.

# Python — Jagged array
jagged = [
    [1, 2, 3],
    [4, 5],
    [6, 7, 8, 9],
    [10]
]

# Access
print(jagged[1][1])  # Output: 5 (row 1, col 1)
print(jagged[2][3])  # Output: 9 (row 2, col 3)

# Iterate
for i, row in enumerate(jagged):
    print(f"Row {i}: {row}")

Jagged Arrays in Java

// Java — Jagged array
int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[4];
jagged[2] = new int[3];

// Initialize
jagged[0][0] = 10;
jagged[0][1] = 20;
jagged[1][0] = 30;
jagged[1][1] = 40;
jagged[1][2] = 50;
jagged[1][3] = 60;
jagged[2][0] = 70;
jagged[2][1] = 80;
jagged[2][2] = 90;

// Or initialize directly
int[][] jagged = {
    {1, 2},
    {3, 4, 5, 6},
    {7, 8, 9}
};

Jagged Arrays in C#

// C# — Jagged array
int[][] jagged = new int[3][];
jagged[0] = new int[2] { 1, 2 };
jagged[1] = new int[4] { 3, 4, 5, 6 };
jagged[2] = new int[3] { 7, 8, 9 };

// Access
Console.WriteLine(jagged[1][2]);  // Output: 5

Comparison: Rectangular vs Jagged Arrays

AspectRectangular Array (2D/3D)Jagged Array
StructureAll rows have the same lengthEach row can have a different length
MemoryContiguous blockNon-contiguous (each row is separate)
Access SpeedFaster (contiguous memory)Slightly slower (multiple array lookups)
Memory EfficiencyMay waste space if rows are sparseMore memory-efficient for sparse data
Use CasesMatrices, images, uniform dataData with variable row lengths, sparse matrices

N-Dimensional Arrays

Arrays can have more than three dimensions (4D, 5D, etc.), though they are less common. They are used in specialized fields like deep learning, scientific computing, and computer graphics.

# Python — 4D array using NumPy
import numpy as np
arr_4d = np.random.rand(2, 3, 4, 5)  # 2 × 3 × 4 × 5
print(arr_4d.shape)  # Output: (2, 3, 4, 5)

# Access
value = arr_4d[1][2][3][4]

Complexity Summary

Array TypeAccess TimeMemoryUse Case
2D ArrayO(1)m × nMatrices, images, tables
3D ArrayO(1)l × m × nRGB images, volumetric data
Jagged ArrayO(1)Sum of row lengths + overheadVariable-length data, sparse matrices
N-D ArrayO(1)d1 × d2 × ... × dnDeep learning, scientific computing

Applications by Array Type

2D Arrays

  • Image Processing: Grayscale images (pixels).
  • Game Development: Chess boards, tic-tac-toe, Sudoku.
  • Spreadsheets: Tabular data representation.
  • Graph Representation: Adjacency matrices.

3D Arrays

  • Color Images: RGB channels (height × width × 3).
  • Medical Imaging: CT scans, MRI (3D volumetric data).
  • Video Processing: Frames (time × height × width × channels).
  • Scientific Simulations: 3D spatial data (temperature, pressure).

Jagged Arrays

  • Storing variable-length data: Student grades per subject, employee records.
  • Parsing CSV/JSON data: Handling rows with different column counts.
  • Sparse Matrix Representation: Storing only non-zero elements.
  • Graph Representations: Adjacency lists (variable number of neighbors).

Key Takeaways

  • 2D arrays are ideal for uniform grid data (matrices, tables).
  • 3D arrays extend this to multiple layers (RGB images, volumetric data).
  • Jagged arrays provide flexibility for non-uniform data.
  • Choose the array type based on the data structure and memory requirements.

Ready to master Data Structures & Algorithms?

Learn DSA hands-on with mentor-led sessions, real interview practice, and placement support.

Explore Course