Multiplies the array by a constant factor in place. - Java Collection Framework

Java examples for Collection Framework:Array Algorithm

Description

Multiplies the array by a constant factor in place.

Demo Code


//package com.java2s;

public class Main {
    /**//from  w w  w  .  j  a  va 2s. com
     * Multiplies the array by a constant factor in place.
     * @param input
     * @param factor
     * @return
     */
    public static void multiply(double[] input, double factor) {
        for (int i = 0; i < input.length; i++) {
            input[i] = input[i] * factor;
        }
    }

    /**
     * Multiplies each value of the first array with the corresponding
     * value of the second one.
     */
    public static double[] multiply(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