← Back to Home

Two-Dimensional Arrays

A two-dimensional array (2D array) in Java is an array of arrays used to store data in a tabular (row–column) structure. It is commonly used for matrices, grids, tables, and multi-level data processing. This topic is important for logic building, real-time scenarios, and interviews.

What Is a Two-Dimensional Array?

  • Stores data in rows and columns
  • Each element is accessed using two indexes
  • Internally implemented as array of arrays
  • Supports jagged (irregular) structures

Declaration of 2D Array

Syntax

dataType[][] arrayName;
or
dataType arrayName[][];
          

Preferred:

int[][] matrix;
          

Creation of 2D Array

int[][] matrix = new int[3][4];
          
  • 3 rows
  • 4 columns per row
  • Default values are assigned automatically

Default Values in 2D Arrays

Data Type Default Value
int 0
double 0.0
boolean false
char '\u0000'
Object null

Initialization of 2D Array

1. Static Initialization

int[][] matrix = {
    {1, 2, 3},
    {4, 5, 6}
};
          

2. Dynamic Initialization

int[][] matrix = new int[2][3];
matrix[0][0] = 1;
matrix[0][1] = 2;
matrix[0][2] = 3;
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
          

Accessing 2D Array Elements

int value = matrix[1][2];  // 6
          

⚠️ Invalid index results in:

ArrayIndexOutOfBoundsException
          

Length Property in 2D Arrays

int rows = matrix.length;
int cols = matrix[0].length;
          
  • matrix.length → number of rows
  • matrix[row].length → number of columns in that row

Traversing a 2D Array

Using Nested for Loop

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();
}
          

Using Enhanced for-each Loop

for (int[] row : matrix) {
    for (int value : row) {
        System.out.print(value + " ");
    }
    System.out.println();
}
          

Jagged Arrays (Irregular 2D Arrays)

In Java, rows can have different column sizes.

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

Example: Matrix Addition

int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{5, 6}, {7, 8}};
int[][] sum = new int[2][2];
for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
        sum[i][j] = a[i][j] + b[i][j];
    }
}
          

Memory Representation (Conceptual)

  • 2D arrays are arrays of references
  • Each row is a separate array
  • Allows jagged structures

Common Use Cases

  • Matrix operations
  • Tables and grids
  • Game boards
  • Multi-level datasets

Common Beginner Mistakes

  • Assuming all rows have equal columns
  • Incorrect loop bounds
  • Confusing row and column indexes
  • Hardcoding sizes
  • Ignoring jagged array behavior

Interview-Ready Answers

Short Answer

A two-dimensional array stores data in a row–column format using an array of arrays.

Detailed Answer

In Java, a two-dimensional array is implemented as an array of arrays, allowing both rectangular and jagged structures. It is accessed using two indices and commonly used for matrices and tabular data.

Key Takeaway

Two-dimensional arrays provide a structured way to manage tabular data. Understanding their memory model and traversal is essential for efficient and correct Java programs.