Loading

Quipoin Menu

Learn • Practice • Grow

data-structure-with-java / Multi-Dimensional Arrays
tutorial

Multi-Dimensional Arrays

Sometimes you need to represent a grid, a matrix, or a table. Multi-dimensional arrays let you do that. In Java, a 2D array is essentially an array of arrays.

Declaration and initialization


// 2D array with 3 rows, 4 columns
int[][] matrix = new int[3][4];

// Initialize with values
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Accessing elements
int element = grid[1][2]; // 6

Jagged arrays
Rows can have different lengths.


int[][] jagged = new int[3][];
jagged[0] = new int[2];
jagged[1] = new int[3];
jagged[2] = new int[1];

Traversing a 2D array
Use nested loops: row-major order (outer loop for rows, inner for columns).


for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
2D arrays are used for matrices, grids, image processing, dynamic programming tables, etc.
Two Minute Drill
  • Multi-dimensional arrays are arrays of arrays.
  • 2D arrays declared as `type[][] name`.
  • Access via `array[row][col]`.
  • Jagged arrays allow rows of different lengths.
  • Common operations: matrix addition, multiplication, traversal.

Need more clarification?

Drop us an email at career@quipoinfotech.com