集册 Java实例教程 计算一个双精度数组的和。

计算一个双精度数组的和。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

459
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算双精度数组的总和。


//package com.nowjava;

import java.util.List;//from n o w    j a v a  . c o m


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));

    }


    /**

     * 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) {/**from n o w j a v a . c o m - 时  代  Java**/


        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;


    }


    
展开阅读全文