---
title: Add two matrix using multi-dimensional arrays
url: https://docs.vultr.com/java/examples/add-two-matrix-using-multi-dimensional-arrays
publish_date: 2024-11-29T22:40:00.095298Z
last_updated: 2024-11-29T22:40:00.095298Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/1180/9d6fd6b8-3765-4100-843b-28f714b2db9d.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# Java Program to Add Two Matrix Using Multi-dimensional Arrays

## Introduction
Multi-dimensional arrays in Java are a fundamental aspect of data organization allowing for structured data storage like rows and columns, which is ideal for mathematical matrices. Matrices are frequently used in various applications including scientific calculations, graphics transformations, and more. Working with matrices involves a number of operations, one of the most basic of which is matrix addition.

In this article, you will learn how to add two matrices using multi-dimensional arrays in Java. Through well-delineated examples, explore how to define matrices, implement matrix addition, and output the resultant matrix to the console.

## Defining and Initializing Matrices

### Initialize Multi-dimensional Arrays
1. Define two matrices as two-dimensional arrays in Java.
1. Initialize them with predefined values which represent the elements of the matrices.

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

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

### Verify Dimensions
1. Ensure both matrices have the same dimensions.

## Adding Two Matrices

### Create a Method for Matrix Addition
1. Define a method called `addMatrices` that takes two two-dimensional integer arrays as parameters and returns a new two-dimensional integer array containing the result.
1. Include loops to iterate through each element of the arrays to sum corresponding elements of `matrixA` and `matrixB`.

    ```java
    public static int[][] addMatrices(int[][] matrixA, int[][] matrixB) {
        int rows = matrixA.length;
        int cols = matrixA[0].length;
        int[][] resultMatrix = new int[rows][cols];

        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                resultMatrix[i][j] = matrixA[i][j] + matrixB[i][j];
            }
        }
        return resultMatrix;
    }
    ```

In this method, loops iterate through rows and columns of the matrices. The sum of corresponding elements from `matrixA` and `matrixB` is stored in the result matrix.

### Integrate Matrix Addition into Main Method
1. Call the `addMatrices` method from within the `main` method of your Java program, passing `matrixA` and `matrixB` as arguments.

    ```java
    public static void main(String[] args) {
        int[][] result = addMatrices(matrixA, matrixB);
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
    ```

This snippet first calls the addition method for the two matrices and then prints out each element of the resulting matrix, formatting the output as a matrix.

## Conclusion
Adding two matrices using multi-dimensional arrays in Java is an excellent exercise to become comfortable with array manipulation and nested loops. By defining matrices, creating a functional addition method, and managing output, you cover basic operations that are foundational for more complex mathematical tasks in programming. Use this matrix addition as a stepping stone for further exploring Java's capabilities in handling more intricate matrix operations like multiplication or inversion, fostering a deeper understanding of both Java programming and linear algebra concepts.