Multi-Dimensional Arrays

2D arrays and beyond

Interview Relevant: Matrix operations are common in interviews
6 min read

Multi-Dimensional Arrays

Multi-dimensional arrays are arrays of arrays. The most common is the 2D array, which can be thought of as a table with rows and columns.

2D Array Structure

int[][] matrix = new int[3][4];  // 3 rows, 4 columns

// Visual representation:
// [0,0] [0,1] [0,2] [0,3]   ← Row 0
// [1,0] [1,1] [1,2] [1,3]   ← Row 1
// [2,0] [2,1] [2,2] [2,3]   ← Row 2

🔑 Memory Layout: In Java, 2D arrays are stored as "array of arrays" (row-major). Each row is a separate array object in memory.

Accessing Elements

  • array[row][col] - access specific element
  • array.length - number of rows
  • array[i].length - columns in row i

Code Examples

Creating and accessing 2D arrays

java
1// 2D Array Declaration and Initialization
2int[][] matrix;                         // Declaration
3matrix = new int[3][4];                 // 3 rows, 4 columns
4
5// Declaration + creation
6int[][] grid = new int[2][3];
7
8// Initialization with values
9int[][] table = {
10    {1, 2, 3},
11    {4, 5, 6},
12    {7, 8, 9}
13};
14
15// Access elements
16System.out.println(table[0][0]);  // 1 (first row, first col)
17System.out.println(table[2][2]);  // 9 (last row, last col)
18
19// Modify element
20table[1][1] = 100;  // Change center element
21
22// Get dimensions
23System.out.println("Rows: " + table.length);       // 3
24System.out.println("Cols: " + table[0].length);    // 3

Different traversal methods

java
1// Traversing 2D arrays
2int[][] matrix = {
3    {1, 2, 3, 4},
4    {5, 6, 7, 8},
5    {9, 10, 11, 12}
6};
7
8// Method 1: Nested for loops
9for (int i = 0; i < matrix.length; i++) {           // rows
10    for (int j = 0; j < matrix[i].length; j++) {    // cols
11        System.out.print(matrix[i][j] + " ");
12    }
13    System.out.println();
14}
15
16// Method 2: Enhanced for loops
17for (int[] row : matrix) {
18    for (int cell : row) {
19        System.out.print(cell + " ");
20    }
21    System.out.println();
22}
23
24// Print with formatting
25for (int[] row : matrix) {
26    for (int val : row) {
27        System.out.printf("%4d", val);
28    }
29    System.out.println();
30}

Matrix addition and transpose operations

java
1// Matrix operations
2int[][] a = {{1, 2}, {3, 4}};
3int[][] b = {{5, 6}, {7, 8}};
4
5// Matrix addition
6int rows = a.length;
7int cols = a[0].length;
8int[][] sum = new int[rows][cols];
9
10for (int i = 0; i < rows; i++) {
11    for (int j = 0; j < cols; j++) {
12        sum[i][j] = a[i][j] + b[i][j];
13    }
14}
15// sum = {{6, 8}, {10, 12}}
16
17// Matrix transpose
18int[][] original = {{1, 2, 3}, {4, 5, 6}};  // 2x3
19int[][] transposed = new int[3][2];          // 3x2
20
21for (int i = 0; i < original.length; i++) {
22    for (int j = 0; j < original[i].length; j++) {
23        transposed[j][i] = original[i][j];
24    }
25}

3D arrays and beyond

java
1// 3D Arrays and beyond
2int[][][] cube = new int[2][3][4];  // 2 layers, 3 rows, 4 cols
3
4// Initialize with values
5int[][][] threeDim = {
6    {{1, 2}, {3, 4}},
7    {{5, 6}, {7, 8}}
8};
9
10// Access: cube[layer][row][col]
11System.out.println(threeDim[0][0][0]);  // 1
12System.out.println(threeDim[1][1][1]);  // 8
13
14// Traverse 3D array
15for (int[][] layer : threeDim) {
16    for (int[] row : layer) {
17        for (int cell : row) {
18            System.out.print(cell + " ");
19        }
20        System.out.println();
21    }
22    System.out.println("---");
23}

Use Cases

  • Representing grids and game boards
  • Image processing (pixel matrices)
  • Spreadsheet data
  • Graph adjacency matrices
  • Scientific computations
  • Database table representations

Common Mistakes to Avoid

  • Confusing row and column indices
  • Assuming all rows have same length
  • Off-by-one errors in nested loops
  • Not handling empty arrays
  • Incorrect bounds checking
  • Memory issues with very large arrays