2D / Matrix
Learn 2D / Matrix (Arrays, Data Structures & Algorithms) with worked examples, diagrams and interview questions. Free on JavaSchoolLab.
What is a 2D Array? A 2D array is "an array of arrays" - grid[i] is itself an array (a row), so grid[i][j] means "column j of row i". Rectangular (rows all the same length) is the common case, but Java allows jagged arrays where each row has a different length. grid.length = number of rows; grid[0].length = number of columns in row 0. Traversal is almost always a nested loop: outer loop over rows, inner loop over columns.