Lesson 29 of 50 – Java Multidimensional Arrays
58%

Java Multidimensional Arrays

A multidimensional array is an array whose elements are themselves arrays. In Java, a two-dimensional array is commonly used to store data in rows and columns, like a table.

1. What Is a Multidimensional Array?

A one-dimensional array stores values in a single sequence. A two-dimensional array organizes values into rows and columns.

One-dimensional array:

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

Two-dimensional array:

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

This array contains three rows and three columns.

A two-dimensional array is useful for representing tables, student marks, matrices, and other row-column data.
2. Declaring a Two-Dimensional Array

The common declaration syntax is:

dataType[][] arrayName;

Examples:

int[][] numbers;
String[][] names;
double[][] prices;

These declarations specify the type of each element. The array must be created before you can access its elements.

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

This creates an array with three rows and four columns. All integer elements initially contain 0.

3. Initializing a Two-Dimensional Array

You can initialize a two-dimensional array with values.

int[][] numbers = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Each inner array represents one row.

Row / Column Column 0 Column 1 Column 2
Row 0 1 2 3
Row 1 4 5 6
Row 2 7 8 9
4. Accessing Array Elements

To access an element, use two indexes: the row index and the column index.

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

System.out.println(numbers[0][0]);
System.out.println(numbers[1][2]);
System.out.println(numbers[2][1]);

Output:

10
60
80

Remember that both row and column indexes start at 0.

5. Changing Array Elements

You can update an element by assigning a new value to its indexes.

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

numbers[0][1] = 100;

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

Output:

100

The element at row 0, column 1 changes from 20 to 100.

Another example:

int[][] marks = {
    {70, 80},
    {60, 90}
};

marks[1][0] = 85;

System.out.println(marks[1][0]);

Output: 85

6. Using Nested For Loops

Nested loops are commonly used to access every element in a two-dimensional array.

public class Main {
    public static void main(String[] args) {

        int[][] numbers = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        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:

1 2 3
4 5 6
7 8 9

The outer loop visits each row. The inner loop visits every element in that row.

7. Using the Length Property

The length property gives the number of rows. Each row has its own length.

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

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

Output:

3
3
3
  • numbers.length returns the number of rows.
  • numbers[0].length returns the number of elements in row 0.
Use numbers[i].length when processing each row. This also works when different rows have different lengths.
8. Display a Student Marks Table

A two-dimensional array can store marks for several students across different subjects.

public class Main {
    public static void main(String[] args) {

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

        for (int i = 0; i < marks.length; i++) {
            System.out.print("Student " + (i + 1) + ": ");

            for (int j = 0; j < marks[i].length; j++) {
                System.out.print(marks[i][j] + " ");
            }

            System.out.println();
        }
    }
}

Output:

Student 1: 80 75 90
Student 2: 65 85 70
Student 3: 90 95 88

Each row represents one student's marks in three subjects.

9. Calculate the Sum of All Elements

Calculate the total of all values in a two-dimensional array.

public class Main {
    public static void main(String[] args) {

        int[][] numbers = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        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("Total = " + sum);
    }
}

Output:

Total = 45

The nested loops visit each element and add it to the running total.

10. Find the Largest Element

Find the largest value in a two-dimensional array.

public class Main {
    public static void main(String[] args) {

        int[][] numbers = {
            {10, 25, 30},
            {45, 15, 60},
            {20, 80, 35}
        };

        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 = 80

This example assumes the array has at least one row and that the first row contains at least one element.

11. Jagged Arrays in Java

A jagged array is a two-dimensional array whose rows can have different lengths.

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

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

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

Each row is a separate array, so its length can be different. The row must be initialized before accessing its elements.

12. Three-Dimensional Arrays

A three-dimensional array adds another level of indexing. It can be thought of as a collection of two-dimensional arrays.

int[][][] numbers = {
    {
        {1, 2},
        {3, 4}
    },
    {
        {5, 6},
        {7, 8}
    }
};

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

Output:

1
8

The indexes represent:

  • First index: outer array.
  • Second index: row within the selected two-dimensional array.
  • Third index: element within that row.
13. Practice Assignments

Write Java programs to complete these assignments.

  1. Create a 2D array with three rows and three columns.
  2. Print all elements using nested for loops.
  3. Print the element at row 1, column 2.
  4. Change one element and print the updated array.
  5. Calculate the sum of all array elements.
  6. Find the largest value in a 2D array.
  7. Calculate the sum of each row separately.
  8. Store and display marks for four students in three subjects.
  9. Create a jagged array with three rows of different lengths.
  10. Create a three-dimensional array and access selected elements.
Tip: Practice row indexes, column indexes, nested loops, and the length property before moving on to array sorting and searching.

🧠 Quick Quiz

1. How do you access the first element in the first row of a 2D array named numbers?




2. What does numbers.length return for a 2D array?




3. What is a jagged array?