Java Array Sum sum(double[] array)

Here you can find the source of sum(double[] array)

Description

Computes the sum of an array of doubles.

License

Open Source License

Parameter

Parameter Description
array Array of doubles to be added up.

Return

Sum of the array.

Declaration

public static double sum(double[] array) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**/* ww w.java  2s  . co m*/
     * Computes the sum of an array of doubles.
     *
     * @param array   Array of doubles to be added up.
     * @return   Sum of the array.
     */
    public static double sum(double[] array) {

        double sum = 0;
        for (double d : array) {
            sum += d;
        }

        return sum;
    }

    /**
     * Computes the sum of an array of integers.
     *
     * @param array   Array of integers to be added up.
     * @return   Sum of the array.
     */
    public static int sum(int[] array) {

        int sum = 0;
        for (int i : array) {
            sum += i;
        }

        return sum;
    }

    /**
     * Computes the sum of an array of floats.
     *
     * @param array   Array of floats to be added up.
     * @return   Sum of the array.
     */
    public static float sum(float[] array) {

        int sum = 0;
        for (float f : array) {
            sum += f;
        }

        return sum;
    }

    /**
     * Computes the sum of an array of longs.
     *
     * @param array   Array of longs to be added up.
     * @return   Sum of the array.
     */
    public static long sum(long[] array) {

        long sum = 0;
        for (long l : array) {
            sum += l;
        }

        return sum;
    }

    /**
     * Computes the sum of an array of bytes.
     *
     * @param array   Array of bytes to be added up.
     * @return   Sum of the array.
     */
    public static int sum(byte[] array) {

        int sum = 0;
        for (byte b : array) {
            sum += b;
        }

        return sum;
    }

    /**
     * Computes the sum of values in a matrix of integers.
     *
     * @param matrix Matrix of integers.
     * @return Sum of the matrix values.
     */
    public static int sum(int[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        int sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }

        return sum;

    }

    /**
     * Computes the sum of values in a matrix of doubles.
     *
     * @param matrix Matrix of doubles.
     * @return Sum of the matrix values.
     */
    public static double sum(double[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        double sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }

        return sum;

    }

    /**
     * Computes the sum of values in a matrix of longs.
     *
     * @param matrix Matrix of longs.
     * @return Sum of the matrix values.
     */
    public static long sum(long[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        long sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }

        return sum;

    }

    /**
     * Computes the sum of values in a matrix of floats.
     *
     * @param matrix Matrix of floats.
     * @return Sum of the matrix values.
     */
    public static float sum(float[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        float sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }

        return sum;

    }

    /**
     * Computes the sum of values in a matrix of bytes.
     *
     * @param matrix Matrix of bytes.
     * @return Sum of the matrix values.
     */
    public static int sum(byte[][] matrix) {

        int width = matrix.length;
        int height = matrix[0].length;
        int sum = 0;

        for (int j = 0; j < width; j++) {
            for (int i = 0; i < height; i++) {
                sum += matrix[j][i];
            }
        }

        return sum;

    }

    /**
     *
     * @param array
     * @return
     */
    public static int sum(List<Integer> array) {

        int sum = 0;
        for (int f : array) {
            sum += f;
        }

        return sum;
    }
}

Related

  1. sum(double[] a, double[] b)
  2. sum(double[] a, double[] b)
  3. sum(double[] aArray)
  4. sum(double[] addends)
  5. sum(double[] array)
  6. sum(Double[] array)
  7. sum(double[] array)
  8. sum(double[] array)
  9. sum(double[] array)