Calculates the sum of all the elements in the matrix - Java java.lang

Java examples for java.lang:Math Matrix

Description

Calculates the sum of all the elements in the matrix

Demo Code


//package com.java2s;

public class Main {
    /** /*from  w w w  .  ja v a 2 s . c o m*/
     * Calculates the sum of all the elements in the matrix
     * @param matrix Matrix whose sum is to be calculated
     * @return Double containing the sum of the given matrix
     */
    public static <T> double getMatrixSum(T[][] matrix) {

        double sum = 0.0;

        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix[i].length; j++) {

                sum = sum + (Double) matrix[i][j];
            }
        }

        return sum;
    }
}

Related Tutorials