Multi-dimensional arrays are data structures that organize elements in multiple dimensions, creating matrices, cubes, and higher-dimensional structures. Think of them as grids within grids.
What Are Multi-Dimensional Arrays?
A multi-dimensional array is essentially an array of arrays. While a 1D array is like a list, a 2D array is like a table with rows and columns, and a 3D array is like a cube of data.
Click a button to visualize different array types!
LEVEL 2: MEMORY LAYOUT
Understanding how multi-dimensional arrays are stored in memory is crucial for writing efficient code and avoiding common pitfalls.
Row-Major vs Column-Major Order
Different programming languages store 2D arrays in memory differently. C, C++, and most languages use row-major order, while Fortran uses column-major order.
Values shown for contiguous allocation. Click to compare with fragmented allocation.
LEVEL 4: LANGUAGE IMPLEMENTATIONS
Different programming languages handle multi-dimensional arrays in unique ways, each with their own syntax and optimizations.
C/C++
// Static 2D array in Cint matrix[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};// Dynamic allocation with mallocint** create2DArray(int rows, int cols) {int** arr = malloc(rows * sizeof(int*));for(int i = 0; i < rows; i++) arr[i] = malloc(cols * sizeof(int));return arr;}
Python
# List comprehension for 2D arraymatrix = [[0for _ in range(4)] for _ in range(3)]# NumPy arrays (much more efficient)import numpy as npmatrix = np.zeros((3, 4), dtype=np.int32)matrix = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
Java
// Java 2D array declaration and initializationint[][] matrix = new int[3][4];// Jagged arrays (different row sizes)int[][] jaggedArray = new int[3][];jaggedArray[0] = new int[2];jaggedArray[1] = new int[4];jaggedArray[2] = new int[3];