Write a Java Program to Add Two Matrix Using Multi-dimensional Arrays

Java provides built-in support for multi-dimensional arrays which makes it easy to perform operations on matrices.

Adding two matrices is a fundamental operation in linear algebra and can be performed using multi-dimensional arrays in Java.

In this tutorial, we will walk through the steps to write a Java program to add two matrices.


Before we dive into the program, let’s first understand what a matrix is.

A matrix is a rectangular array of numbers arranged in rows and columns.

It can be represented using a two-dimensional array in Java.

In other words, each row of the matrix is an array, and the entire matrix is an array of arrays.

To add two matrices, we need to perform the addition operation on each corresponding element of the matrices.

That is, we add the elements at position (i,j) of matrix A and matrix B to get the element at position (i,j) of the resulting matrix.

Let’s now write a Java program to add two matrices using multi-dimensional arrays.

Here is the code:

public class MatrixAddition {
   public static void main(String[] args) {
      int rows = 3, columns = 3;
      int[][] matrixA = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
      int[][] matrixB = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
      int[][] result = new int[rows][columns];

      // Add the matrices
      for (int i = 0; i < rows; i++) {
         for (int j = 0; j < columns; j++) {
            result[i][j] = matrixA[i][j] + matrixB[i][j];
         }
      }

      // Display the result
      System.out.println("Matrix A:");
      displayMatrix(matrixA);
      System.out.println("Matrix B:");
      displayMatrix(matrixB);
      System.out.println("Result:");
      displayMatrix(result);
   }

   // Helper method to display a matrix
   public static void displayMatrix(int[][] matrix) {
      for (int[] row : matrix) {
         for (int element : row) {
            System.out.print(element + " ");
         }
         System.out.println();
      }
      System.out.println();
   }
}

In this program, we first define the size and elements of two matrices, matrixA and matrixB. We then create a new matrix, result, to store the sum of the two matrices.

We then iterate over each element of the matrices using two nested for loops, and add the corresponding elements of matrixA and matrixB to get the corresponding element of result.

Finally, we display the matrices using a helper method, displayMatrix, which simply iterates over the matrix and prints each element.

This program outputs the following:

Matrix A:
1 2 3 
4 5 6 
7 8 9 

Matrix B:
9 8 7 
6 5 4 
3 2 1 

Result:
10 10 10 
10 10 10 
10 10 10

As we can see, the program correctly adds the two matrices and displays the result.


In conclusion, adding two matrices using multi-dimensional arrays in Java is a simple and straightforward operation.

By using nested for loops to iterate over each element of the matrices, we can easily perform the addition operation and obtain the result.