Java Array Sum sum(Number[] array)

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

Description

Returns sum of all the elements in the array.

License

Open Source License

Parameter

Parameter Description
array the array to work on

Return

the sum

Declaration

public static double sum(Number[] array) 

Method Source Code

//package com.java2s;
/*//w w  w  .  j  a  v a2 s. c  om
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns sum of all the elements in the array.
     *
     * @param array   the array to work on
     * @return      the sum
     */
    public static double sum(Number[] array) {
        double result;
        int i;

        result = 0.0;
        for (i = 0; i < array.length; i++)
            result += array[i].doubleValue();

        return result;
    }

    /**
     * Returns sum of all the elements in the array.
     *
     * @param array   the array to work on
     * @return      the sum
     */
    public static double sum(int[] array) {
        return sum(toNumberArray(array));
    }

    /**
     * Returns sum of all the elements in the array.
     *
     * @param array   the array to work on
     * @return      the sum
     */
    public static double sum(double[] array) {
        return sum(toNumberArray(array));
    }

    /**
     * Turns the byte array into a Byte array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(byte[] array) {
        Byte[] result;
        int i;

        result = new Byte[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Byte(array[i]);

        return result;
    }

    /**
     * Turns the short array into a Short array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(short[] array) {
        Short[] result;
        int i;

        result = new Short[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Short(array[i]);

        return result;
    }

    /**
     * Turns the int array into a Integer array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(int[] array) {
        Integer[] result;
        int i;

        result = new Integer[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Integer(array[i]);

        return result;
    }

    /**
     * Turns the long array into a Long array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(long[] array) {
        Long[] result;
        int i;

        result = new Long[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Long(array[i]);

        return result;
    }

    /**
     * Turns the float array into a Float array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(float[] array) {
        Float[] result;
        int i;

        result = new Float[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Float(array[i]);

        return result;
    }

    /**
     * Turns the double array into a Double array.
     *
     * @param array   the array to convert
     * @return      the converted array
     */
    public static Number[] toNumberArray(double[] array) {
        Double[] result;
        int i;

        result = new Double[array.length];
        for (i = 0; i < array.length; i++)
            result[i] = new Double(array[i]);

        return result;
    }
}

Related

  1. sum(long... values)
  2. sum(long[] array)
  3. sum(long[] array)
  4. sum(long[] counts)
  5. sum(Number... numbers)
  6. sum(Number[] numbers)
  7. sum(Object[] srcOne, Object[] srcTwo)
  8. sum(short tab[], int a, int b)
  9. sum(short[] ary)