Java List Subtract subtract(List a, List b)

Here you can find the source of subtract(List a, List b)

Description

Vector subtraction Modifies the first parameter but also returns it for convenience.

License

Open Source License

Parameter

Parameter Description
a Vector to subtract from
b Vector to subtract

Return

Result

Declaration

public static List<Float> subtract(List<Float> a, List<Float> b) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * This file is part of Tmetrics./*from ww w .ja v a  2s  .co  m*/
 *
 * Tmetrics 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.
 *
 * Tmetrics 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 Tmetrics. If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Vector subtraction
     * 
     * Modifies the first parameter but also returns it for convenience. Vectors
     * must be of same length.
     * 
     * @param a
     *            Vector to subtract from
     * @param b
     *            Vector to subtract
     * @return Result
     */
    public static List<Float> subtract(List<Float> a, List<Float> b) {
        if (a.size() != b.size()) {
            throw new IllegalArgumentException(
                    "Length of vector a (" + a.size() + ") does not equal length of vector b (" + b.size() + ")");
        }
        List<Float> c = new ArrayList<Float>();

        for (int i = 0; i < a.size(); i++) {
            c.add(a.get(i) - b.get(i));
        }
        return c;
    }
}

Related

  1. subtract(final List list1, final List list2)
  2. subtract(final List list1, final List list2)
  3. subtract(final List result, final Object o1, final Object o2)
  4. subtract(final String[] input, final String[] list)
  5. subtract(List vector, int[] sparseVector)
  6. subtract(List a, List b)
  7. subtract(List aList, List bList)
  8. subtract(String[] list1, String[] list2)