CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Multi-Dimensional Array in Java

Introduction

A Java two-dimensional array is an array whose elements refer to other arrays. We can use it for rows of marks or a simple seating plan. Each row is a separate array, so rows can have different lengths.

The most commonly used multi-dimensional array is a two-dimensional array, which can be visualized as rows and columns.

For example, a matrix can be represented as:

        Column
          0    1    2
       +----+----+----+
Row 0  | 10 | 20 | 30 |
       +----+----+----+
Row 1  | 40 | 50 | 60 |
       +----+----+----+
Row 2  | 70 | 80 | 90 |
       +----+----+----+

In Java, we can create this using:

int[][] numbers = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};

Here:

  • numbers is a two-dimensional array.
  • It contains 3 rows.
  • Each row contains 3 elements.
  • The first index represents the row.
  • The second index represents the column.

For example:

numbers[0][1]

returns:

20

What is a Multi-Dimensional Array?

A multi-dimensional array is an array with more than one dimension.

Java supports arrays with multiple dimensions, such as:

int[][] array2D;
int[][][] array3D;

The most common type is the two-dimensional array.

int[][] numbers;

A two-dimensional array can be visualized as:

          Column
        0    1    2
      +----+----+----+
  0   | 10 | 20 | 30 |
      +----+----+----+
  1   | 40 | 50 | 60 |
      +----+----+----+
  2   | 70 | 80 | 90 |
      +----+----+----+
       Row

The first index represents the row:

numbers[1]

The second index represents the column:

numbers[1][2]

Why Do We Need Multi-Dimensional Arrays?

A single-dimensional array is useful for storing data in a sequence.

For example:

int[] marks = {80, 75, 90, 85};

But sometimes data naturally belongs to rows and columns.

For example, student marks for different subjects:

             Java  SQL  Spring
Student 1     80   75     90
Student 2     85   88     92
Student 3     70   80     75

This type of data can be represented using a two-dimensional array:

int[][] marks = {
    {80, 75, 90},
    {85, 88, 92},
    {70, 80, 75}
};

Declaring a Two-Dimensional Array

The syntax is:

dataType[][] arrayName;

Example

int[][] numbers;

Another valid syntax is:

int numbers[][];

The following form is generally easier to read:

int[][] numbers;

Creating a Two-Dimensional Array

We can create a two-dimensional array using the new keyword.

int[][] numbers = new int[3][4];

This creates:

3 rows
4 columns

So the total number of elements is:

3 × 4 = 12

The structure can be visualized as:

        0    1    2    3
     +----+----+----+----+
  0  |  0 |  0 |  0 |  0 |
     +----+----+----+----+
  1  |  0 |  0 |  0 |  0 |
     +----+----+----+----+
  2  |  0 |  0 |  0 |  0 |
     +----+----+----+----+

Program 1: Creating a Two-Dimensional Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = new int[3][4];
 
        System.out.println("Rows: " + numbers.length);
        System.out.println("Columns: " + numbers[0].length);
    }
}

Output

Rows: 3
Columns: 4

Here:

numbers.length

returns the number of rows.

And:

numbers[0].length

returns the number of columns in the first row.

Initializing a Two-Dimensional Array

We can assign values using row and column indexes.

int[][] numbers = new int[2][3];
 
numbers[0][0] = 10;
numbers[0][1] = 20;
numbers[0][2] = 30;
 
numbers[1][0] = 40;
numbers[1][1] = 50;
numbers[1][2] = 60;

The array looks like:

        0    1    2
     +----+----+----+
  0  | 10 | 20 | 30 |
     +----+----+----+
  1  | 40 | 50 | 60 |
     +----+----+----+

Program 2: Initializing a Two-Dimensional Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = new int[2][3];
 
        numbers[0][0] = 10;
        numbers[0][1] = 20;
        numbers[0][2] = 30;
 
        numbers[1][0] = 40;
        numbers[1][1] = 50;
        numbers[1][2] = 60;
 
        System.out.println(numbers[0][0]);
        System.out.println(numbers[0][1]);
        System.out.println(numbers[0][2]);
 
        System.out.println(numbers[1][0]);
        System.out.println(numbers[1][1]);
        System.out.println(numbers[1][2]);
    }
}

Output

10
20
30
40
50
60

Initializing a Two-Dimensional Array Using Array Literals

When values are already known, we can initialize the array directly.

int[][] numbers = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};

This creates a two-dimensional array with:

3 rows
3 columns

Program 3: Array Literal Initialization

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        System.out.println(numbers[0][0]);
        System.out.println(numbers[1][1]);
        System.out.println(numbers[2][2]);
    }
}

Output

10
50
90

Understanding Row and Column Index

Consider:

int[][] numbers = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};

The indexes are:

              Column
              0    1    2
           +----+----+----+
Row 0      | 10 | 20 | 30 |
           +----+----+----+
Row 1      | 40 | 50 | 60 |
           +----+----+----+
Row 2      | 70 | 80 | 90 |
           +----+----+----+

For example:

numbers[0][0]

means:

Row 0, Column 0

and returns:

10

Similarly:

numbers[2][1]

means:

Row 2, Column 1

and returns:

80

Program 4: Accessing Elements

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        System.out.println("First element: " + numbers[0][0]);
        System.out.println("Middle element: " + numbers[1][1]);
        System.out.println("Last element: " + numbers[2][2]);
    }
}

Output

First element: 10
Middle element: 50
Last element: 90

Modifying Elements in a Two-Dimensional Array

We can modify an element using its row and column indexes.

numbers[1][1] = 500;

If the original array is:

10  20  30
40  50  60
70  80  90

after:

numbers[1][1] = 500;

it becomes:

10  20   30
40  500  60
70  80   90

Program 5: Modifying an Element

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        System.out.println("Before: " + numbers[1][1]);
 
        numbers[1][1] = 500;
 
        System.out.println("After: " + numbers[1][1]);
    }
}

Output

Before: 50
After: 500

Traversing a Two-Dimensional Array

To visit every element of a two-dimensional array, we commonly use nested loops.

A nested loop means one loop is placed inside another loop.

For example:

for (int i = 0; i < numbers.length; i++) {
 
    for (int j = 0; j < numbers[i].length; j++) {
 
        System.out.println(numbers[i][j]);
    }
}

The outer loop handles the rows.

The inner loop handles the columns.

Program 6: Traversing Using Nested for Loop

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                System.out.print(numbers[i][j] + " ");
            }
 
            System.out.println();
        }
    }
}

Output

10 20 30
40 50 60
70 80 90

How Nested Loops Work

For this array:

10 20 30
40 50 60
70 80 90

the outer loop starts with:

i = 0

The inner loop processes:

numbers[0][0]
numbers[0][1]
numbers[0][2]

Then the outer loop moves to:

i = 1

The inner loop processes:

numbers[1][0]
numbers[1][1]
numbers[1][2]

Finally:

i = 2

processes:

numbers[2][0]
numbers[2][1]
numbers[2][2]

Traversing Using Enhanced for Loop

A two-dimensional array can also be traversed using nested enhanced for loops.

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

The outer loop gets one row at a time.

The inner loop gets each element from that row.

Program 7: Enhanced for Loop

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        for (int[] row : numbers) {
 
            for (int value : row) {
 
                System.out.print(value + " ");
            }
 
            System.out.println();
        }
    }
}

Output

10 20 30
40 50 60
70 80 90

Finding the Number of Rows

The length property of a two-dimensional array gives the number of rows.

int[][] numbers = {
    {10, 20, 30},
    {40, 50, 60},
    {70, 80, 90}
};
 
System.out.println(numbers.length);

Output:

3

Finding the Number of Columns

For a regular two-dimensional array, we can use:

numbers[0].length

Example

System.out.println(numbers[0].length);

Output:

3

So:

numbers.length       → number of rows
numbers[0].length    → number of columns in row 0

For a rectangular array where every row has the same number of columns, this gives the column count.

Program 8: Find Rows and Columns

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        int rows = numbers.length;
        int columns = numbers[0].length;
 
        System.out.println("Rows = " + rows);
        System.out.println("Columns = " + columns);
    }
}

Output

Rows = 3
Columns = 3

Taking Two-Dimensional Array Input

We can use Scanner to read values from the user.

For example, if we want 2 rows and 3 columns:

int[][] numbers = new int[2][3];

Then we can use nested loops to read each value.

Program 9: Read a Two-Dimensional Array from User

import java.util.Scanner;
 
public class HelloWorld {
 
    public static void main(String[] args) {
 
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("Enter number of rows: ");
        int rows = scanner.nextInt();
 
        System.out.print("Enter number of columns: ");
        int columns = scanner.nextInt();
 
        int[][] numbers = new int[rows][columns];
 
        System.out.println("Enter array elements:");
 
        for (int i = 0; i < rows; i++) {
 
            for (int j = 0; j < columns; j++) {
 
                numbers[i][j] = scanner.nextInt();
            }
        }
 
        System.out.println("Array:");
 
        for (int i = 0; i < rows; i++) {
 
            for (int j = 0; j < columns; j++) {
 
                System.out.print(numbers[i][j] + " ");
            }
 
            System.out.println();
        }
 
        scanner.close();
    }
}

Example Input

Enter number of rows: 2
Enter number of columns: 3
Enter array elements:
10
20
30
40
50
60

Output

Array:
10 20 30
40 50 60

Program 10: Find the Sum of All Elements

We can calculate the sum using nested loops.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60}
        };
 
        int sum = 0;
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                sum += numbers[i][j];
            }
        }
 
        System.out.println("Sum = " + sum);
    }
}

Output

Sum = 210

Calculation:

10 + 20 + 30 + 40 + 50 + 60 = 210

Program 11: Find the Largest Element

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 45, 30},
            {80, 20, 60},
            {35, 90, 15}
        };
 
        int largest = numbers[0][0];
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                if (numbers[i][j] > largest) {
 
                    largest = numbers[i][j];
                }
            }
        }
 
        System.out.println("Largest = " + largest);
    }
}

Output

Largest = 90

We start with:

int largest = numbers[0][0];

Then every element is compared with the current largest value.

Program 12: Find the Smallest Element

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 45, 30},
            {80, 20, 60},
            {35, 90, 15}
        };
 
        int smallest = numbers[0][0];
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                if (numbers[i][j] < smallest) {
 
                    smallest = numbers[i][j];
                }
            }
        }
 
        System.out.println("Smallest = " + smallest);
    }
}

Output

Smallest = 10

Program 13: Find the Sum of Each Row

Sometimes we need to calculate the sum separately for each row.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        for (int i = 0; i < numbers.length; i++) {
 
            int sum = 0;
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                sum += numbers[i][j];
            }
 
            System.out.println(
                "Sum of Row " + i + " = " + sum
            );
        }
    }
}

Output

Sum of Row 0 = 60
Sum of Row 1 = 150
Sum of Row 2 = 240

Program 14: Find the Sum of Each Column

We can also calculate the sum of each column.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        int rows = numbers.length;
        int columns = numbers[0].length;
 
        for (int j = 0; j < columns; j++) {
 
            int sum = 0;
 
            for (int i = 0; i < rows; i++) {
 
                sum += numbers[i][j];
            }
 
            System.out.println(
                "Sum of Column " + j + " = " + sum
            );
        }
    }
}

Output

Sum of Column 0 = 120
Sum of Column 1 = 150
Sum of Column 2 = 180

Program 15: Search for an Element

We can search for an element using nested loops.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        int search = 50;
 
        boolean found = false;
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                if (numbers[i][j] == search) {
 
                    System.out.println(
                        "Element found at row " + i +
                        ", column " + j
                    );
 
                    found = true;
                    break;
                }
            }
 
            if (found) {
                break;
            }
        }
 
        if (!found) {
 
            System.out.println("Element not found");
        }
    }
}

Output

Element found at row 1, column 1

Program 16: Print the Main Diagonal

Consider the following square matrix:

10  20  30
40  50  60
70  80  90

The main diagonal is:

10
   50
      90

The indexes are:

[0][0]
[1][1]
[2][2]

So the row index and column index are the same.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] matrix = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        System.out.println("Main Diagonal:");
 
        for (int i = 0; i < matrix.length; i++) {
 
            System.out.println(matrix[i][i]);
        }
    }
}

Output

Main Diagonal:
10
50
90

Program 17: Find the Sum of Main Diagonal

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] matrix = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        int sum = 0;
 
        for (int i = 0; i < matrix.length; i++) {
 
            sum += matrix[i][i];
        }
 
        System.out.println("Diagonal Sum = " + sum);
    }
}

Output

Diagonal Sum = 150

Calculation:

10 + 50 + 90 = 150

Program 18: Print the Secondary Diagonal

For the matrix:

10  20  30
40  50  60
70  80  90

the secondary diagonal is:

      30
   50
70

The indexes are:

[0][2]
[1][1]
[2][0]

The column index can be calculated using:

column = length - 1 - row

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] matrix = {
            {10, 20, 30},
            {40, 50, 60},
            {70, 80, 90}
        };
 
        int size = matrix.length;
 
        System.out.println("Secondary Diagonal:");
 
        for (int i = 0; i < size; i++) {
 
            System.out.println(
                matrix[i][size - 1 - i]
            );
        }
    }
}

Output

Secondary Diagonal:
30
50
70

Program 19: Add Two Matrices

Two matrices of the same dimensions can be added element by element.

Consider:

Matrix A:
 
10  20
30  40

and:

Matrix B:
 
1   2
3   4

Their sum is:

11  22
33  44

Program:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] first = {
            {10, 20},
            {30, 40}
        };
 
        int[][] second = {
            {1, 2},
            {3, 4}
        };
 
        int rows = first.length;
        int columns = first[0].length;
 
        int[][] result = new int[rows][columns];
 
        for (int i = 0; i < rows; i++) {
 
            for (int j = 0; j < columns; j++) {
 
                result[i][j] =
                    first[i][j] + second[i][j];
            }
        }
 
        System.out.println("Result:");
 
        for (int i = 0; i < rows; i++) {
 
            for (int j = 0; j < columns; j++) {
 
                System.out.print(result[i][j] + " ");
            }
 
            System.out.println();
        }
    }
}

Output

Result:
11 22
33 44

Program 20: Transpose of a Matrix

The transpose of a matrix is created by converting rows into columns and columns into rows.

Original matrix:

10  20  30
40  50  60

Transpose:

10  40
20  50
30  60

Program:

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] matrix = {
            {10, 20, 30},
            {40, 50, 60}
        };
 
        int rows = matrix.length;
        int columns = matrix[0].length;
 
        int[][] transpose = new int[columns][rows];
 
        for (int i = 0; i < rows; i++) {
 
            for (int j = 0; j < columns; j++) {
 
                transpose[j][i] = matrix[i][j];
            }
        }
 
        System.out.println("Transpose:");
 
        for (int i = 0; i < transpose.length; i++) {
 
            for (int j = 0; j < transpose[i].length; j++) {
 
                System.out.print(transpose[i][j] + " ");
            }
 
            System.out.println();
        }
    }
}

Output

Transpose:
10 40
20 50
30 60

Jagged Arrays in Java

Java does not require every row of a two-dimensional array to have the same number of columns.

Such an array is called a jagged array.

For example:

int[][] numbers = {
    {10, 20},
    {30, 40, 50},
    {60, 70, 80, 90}
};

Here:

Row 0 → 2 elements
Row 1 → 3 elements
Row 2 → 4 elements

The structure is:

10  20
 
30  40  50
 
60  70  80  90

Program 21: Jagged Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = {
            {10, 20},
            {30, 40, 50},
            {60, 70, 80, 90}
        };
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                System.out.print(numbers[i][j] + " ");
            }
 
            System.out.println();
        }
    }
}

Output

10 20
30 40 50
60 70 80 90

Notice that we use:

numbers[i].length

instead of assuming that every row has the same number of columns.

Creating a Jagged Array Using new

We can also create a jagged array step by step.

int[][] numbers = new int[3][];
 
numbers[0] = new int[2];
numbers[1] = new int[3];
numbers[2] = new int[4];

Now:

Row 0 → 2 elements
Row 1 → 3 elements
Row 2 → 4 elements

Program 22: Creating a Jagged Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] numbers = new int[3][];
 
        numbers[0] = new int[2];
        numbers[1] = new int[3];
        numbers[2] = new int[4];
 
        numbers[0][0] = 10;
        numbers[0][1] = 20;
 
        numbers[1][0] = 30;
        numbers[1][1] = 40;
        numbers[1][2] = 50;
 
        numbers[2][0] = 60;
        numbers[2][1] = 70;
        numbers[2][2] = 80;
        numbers[2][3] = 90;
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                System.out.print(numbers[i][j] + " ");
            }
 
            System.out.println();
        }
    }
}

Output

10 20
30 40 50
60 70 80 90

Java Multi-Dimensional Arrays Are Arrays of Arrays

An important concept is that Java's two-dimensional arrays are technically arrays whose elements are themselves arrays.

Consider:

int[][] numbers = new int[3][4];

Conceptually:

numbers
   |
   +---- Row 0 → [0][0] [0][1] [0][2] [0][3]
   |
   +---- Row 1 → [1][0] [1][1] [1][2] [1][3]
   |
   +---- Row 2 → [2][0] [2][1] [2][2] [2][3]

This is also why Java can support jagged arrays.

Each row can be a separate array with a different length.

Three-Dimensional Array

Java also supports arrays with three dimensions.

Syntax:

int[][][] numbers;

Example

int[][][] numbers = new int[2][3][4];

This can be understood as:

2 blocks
3 rows per block
4 columns per row

A three-dimensional array is useful when data has three logical dimensions.

For example:

Building
   ↓
Floor
   ↓
Room

Program 23: Three-Dimensional Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][][] numbers = {
            {
                {1, 2},
                {3, 4}
            },
            {
                {5, 6},
                {7, 8}
            }
        };
 
        for (int i = 0; i < numbers.length; i++) {
 
            for (int j = 0; j < numbers[i].length; j++) {
 
                for (int k = 0; k < numbers[i][j].length; k++) {
 
                    System.out.print(numbers[i][j][k] + " ");
                }
 
                System.out.println();
            }
 
            System.out.println();
        }
    }
}

Output

1 2
3 4
 
5 6
7 8

Each additional dimension requires another level of indexing.

For example:

numbers[i][j][k]

Multi-Dimensional Array and Memory

A two-dimensional Java array is not necessarily stored as one single rectangular memory block.

For example:

int[][] numbers = new int[3][3];

can be viewed conceptually as:

numbers
   |
   +---- Row 0 → int[]
   |
   +---- Row 1 → int[]
   |
   +---- Row 2 → int[]

Each row is itself an array.

This model allows Java to create jagged arrays where different rows have different lengths.

Common Mistakes

Mistake 1: Using the Wrong Index

For:

int[][] numbers = new int[2][3];

valid row indexes are:

0
1

and valid column indexes are:

0
1
2

Therefore:

numbers[2][0]

is invalid.

Mistake 2: Confusing Rows and Columns

For:

int[][] numbers = new int[3][4];

there are:

3 rows
4 columns

So:

numbers.length

returns:

3

while:

numbers[0].length

returns:

4

Mistake 3: Assuming Every Row Has the Same Length

For a jagged array:

int[][] numbers = {
    {10, 20},
    {30, 40, 50},
    {60, 70, 80, 90}
};

do not assume:

numbers[0].length == numbers[1].length

Instead, use:

numbers[i].length

while traversing each row.

Mistake 4: Using Only One Loop

A two-dimensional array normally requires nested loops to process every element.

For example:

for (int i = 0; i < numbers.length; i++) {
 
    for (int j = 0; j < numbers[i].length; j++) {
 
        System.out.println(numbers[i][j]);
    }
}

The outer loop processes rows, while the inner loop processes elements inside each row.

Single Dimensional vs Multi-Dimensional Array

FeatureSingle DimensionalMulti-Dimensional
StructureSingle sequenceMultiple dimensions
Exampleint[]int[][]
IndexesOneTwo or more
Common useList of valuesTables, matrices
TraversalUsually one loopUsually nested loops
Example accessarray[2]array[2][3]

Example

Single Dimensional

int[] numbers = {10, 20, 30};

Access:

numbers[1]

Two Dimensional

int[][] numbers = {
    {10, 20},
    {30, 40}
};

Access:

numbers[1][0]

Practical Example: Student Marks

Suppose three students have marks in three subjects.

             Java  SQL  Spring
Student 1     80   75     90
Student 2     85   88     92
Student 3     70   80     75

We can represent this using:

int[][] marks = {
    {80, 75, 90},
    {85, 88, 92},
    {70, 80, 75}
};

Program 24: Student Marks Using Two-Dimensional Array

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int[][] marks = {
            {80, 75, 90},
            {85, 88, 92},
            {70, 80, 75}
        };
 
        for (int i = 0; i < marks.length; i++) {
 
            int total = 0;
 
            for (int j = 0; j < marks[i].length; j++) {
 
                total += marks[i][j];
            }
 
            double average =
                (double) total / marks[i].length;
 
            System.out.println(
                "Student " + (i + 1)
                + " Total = " + total
                + ", Average = " + average
            );
        }
    }
}

Output

Student 1 Total = 245, Average = 81.66666666666667
Student 2 Total = 265, Average = 88.33333333333333
Student 3 Total = 225, Average = 75.0

This is a practical example of using a two-dimensional array to represent related data in rows and columns.

Key Points to Remember

  • A multi-dimensional array contains multiple dimensions.
  • The most common type is a two-dimensional array.
  • A two-dimensional array is declared using:
int[][] numbers;
  • A two-dimensional array uses two indexes:
numbers[row][column]
  • Java arrays are zero-indexed.
  • array.length gives the number of rows for a two-dimensional array.
  • array[row].length gives the number of elements in a particular row.
  • Nested loops are commonly used to traverse a two-dimensional array.
  • Java supports jagged arrays where rows can have different lengths.
  • A two-dimensional Java array is technically an array of arrays.
  • Java also supports three-dimensional and higher-dimensional arrays.
  • Multi-dimensional arrays are useful for matrices, tables, grids, and other structured data.
  • ArrayIndexOutOfBoundsException can occur when an invalid row or column index is accessed.
  • When working with jagged arrays, use array[i].length instead of assuming every row has the same length.

See you in next page....🙂

← Single Dimensional ArrayArray Methods →
← Single Dimensional ArrayArray Methods →