Adds each value of the first array with the corresponding value of the second one. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Adds each value of the first array with the corresponding value of the second one.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w w w  .  ja v  a  2 s  . c  o m*/
     * Adds each value of the first array with the corresponding
     * value of the second one.
     */
    public static double[] add(double[] first, double[] second) {
        if (first.length != second.length) {
            throw new IllegalArgumentException("Dimensions don't match! "
                    + first.length + " != " + second.length);
        }
        double[] result = new double[first.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = first[i] + second[i];
        }
        return result;
    }
}

Related Tutorials