Java Array Sum sum(byte[] array)

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

Description

Sums the values of elements in an array.

License

Apache License

Parameter

Parameter Description
array the array of values.

Return

the sum of all values in the array.

Declaration

public static int sum(byte[] array) 

Method Source Code

//package com.java2s;
/*//from w  w  w .  j a v  a2 s . c o  m
 * Copyright (c) 2014  Haixing Hu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

public class Main {
    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(byte[] array) {
        int result = 0;
        for (final byte v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(byte[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        int result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(short[] array) {
        int result = 0;
        for (final short v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(short[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        int result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(int[] array) {
        int result = 0;
        for (final int v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static int sum(int[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        int result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static long sum(long[] array) {
        long result = 0;
        for (final long v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static long sum(long[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        long result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static float sum(float[] array) {
        float result = 0;
        for (final float v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static float sum(float[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        float result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @return
     *      the sum of all values in the array.
     */
    public static double sum(double[] array) {
        double result = 0;
        for (final double v : array) {
            result += v;
        }
        return result;
    }

    /**
     * Sums the values of elements in an array.
     *
     * @param array
     *      the array of values.
     * @param startIndex
     *      the index of value starting the sum.
     * @param endIndex
     *      the index next to the value ending the sum.
     * @return
     *      the sum of all values in the array.
     */
    public static double sum(double[] array, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        if (endIndex > array.length) {
            endIndex = array.length;
        }
        double result = 0;
        for (int i = startIndex; i < endIndex; ++i) {
            result += array[i];
        }
        return result;
    }
}

Related

  1. getSum(int[] array)
  2. getSum(int[] array)
  3. sum( int[] a)
  4. sum(byte... bytes)
  5. sum(byte[] array)
  6. sum(byte[] array, int offset, int size)
  7. sum(byte[] array, int offset, int size)
  8. sum(byte[] in, byte[] gamma)
  9. sum(double a[], int begin, int end)