Lesson Completion
Back to course

Multi-Dimensional Arrays: 2D Arrays and Matrices

Beginner
15 minutes4.5Java

1. The Hook (The "Byte-Sized" Intro)

In a Nutshell: 2D arrays are arrays of arrays—grids with rows and columns. Perfect for matrices, game boards, images, and tabular data.

When Chess.com stores a game board: Piece[][] board = new Piece[8][8]. Rows and columns, like a spreadsheet!


2. Conceptual Clarity

💡 The Analogy: Spreadsheet Grid

  • 2D array = Excel spreadsheet
  • Rows = Horizontal (first index)
  • Columns = Vertical (second index)
  • Cell = arr[row][col]
graph TB subgraph "int[][] matrix" R0["Row 0: [1][2][3]"] R1["Row 1: [4][5][6]"] R2["Row 2: [7][8][9]"] end style R0 fill:#2E7D32 style R1 fill:#F57C00 style R2 fill:#1976D2

3. Technical Mastery

Memory Layout (Array of Arrays)

java
int[][] matrix = new int[3][4]; // 3 rows, 4 columns // matrix.length = 3 (rows) // matrix[0].length = 4 (columns)
graph LR Matrix["matrix[][]"] --> Row0["matrix[0] → [_][_][_][_]"] Matrix --> Row1["matrix[1] → [_][_][_][_]"] Matrix --> Row2["matrix[2] → [_][_][_][_]"]

4. Interactive & Applied Code

java
public class TwoDArrayDemo { public static void main(String[] args) { // Declaration and creation int[][] matrix = new int[3][3]; // Static initialization int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Access elements System.out.println("Center: " + grid[1][1]); // 5 System.out.println("Top-left: " + grid[0][0]); // 1 System.out.println("Bottom-right: " + grid[2][2]); // 9 // Modify element grid[1][1] = 100; // Print all elements for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { System.out.print(grid[row][col] + "\t"); } System.out.println(); } // Enhanced for loop for (int[] row : grid) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } // Real-world: Tic-Tac-Toe board char[][] tictactoe = { {'X', 'O', 'X'}, {' ', 'X', 'O'}, {'O', ' ', 'X'} }; // Real-world: Student grades double[][] grades = new double[30][5]; // 30 students, 5 subjects grades[0][0] = 95.5; // Student 0, Subject 0 // Matrix operations int[][] a = {{1, 2}, {3, 4}}; int[][] b = {{5, 6}, {7, 8}}; int[][] sum = new int[2][2]; // Matrix addition for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { sum[i][j] = a[i][j] + b[i][j]; } } // Row and column count System.out.println("Rows: " + grid.length); System.out.println("Cols: " + grid[0].length); } }

⚠️ Common Mistakes

Mistake #1: Confusing row/column order

java
int[][] arr = new int[3][5]; // 3 rows, 5 columns arr[5][3] = 10; // ❌ Wrong! Should be arr[row][col]

Mistake #2: Using wrong length

java
int[][] grid = new int[3][4]; for (int i = 0; i < grid.length; i++) { // ✅ rows for (int j = 0; j < grid[0].length; j++) { // ✅ cols // Use grid[i].length for jagged arrays } }

5. The "Interview Corner"

🏆 Q1: "How is 2D array stored in memory?" Answer: As array of arrays—each row is a separate array object in heap. Not truly contiguous like C/C++.

🏆 Q2: "Transpose a matrix?"

java
for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { int temp = matrix[i][j]; matrix[i][j] = matrix[j][i]; matrix[j][i] = temp; } }

🏆 Q3: "Time complexity to access element?" Answer: O(1)—two pointer dereferences still constant time.


🎓 Key Takeaways

arr[row][col] access pattern
arr.length = rows, arr[0].length = columns
✅ Each row is a separate array object
✅ Static init: {{1,2},{3,4}}
✅ Perfect for grids, matrices, tables

Topics Covered

Java FundamentalsArrays

Tags

#java#arrays#data-structures#multidimensional-arrays#beginner-friendly

Last Updated

2025-02-01