Computes the sum of an array of doubles. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Computes the sum of an array of doubles.

Demo Code


//package com.java2s;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] array = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(sum(array));
    }/*from www  .ja  v  a2 s .c  o 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 Tutorials