Here you can find the source of subtract(List
Parameter | Description |
---|---|
vector | a parameter |
sparseVector | a parameter |
public static List<Integer> subtract(List<Integer> vector, int[] sparseVector)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { /**//from w w w. j a va2 s . co m * The first given vector subtract the second one. * * @param vector1 * @param vector2 * @return * @throws IllegalArgumentException */ public static List<Integer> subtract(List<Integer> vector1, List<Integer> vector2) { if (vector1.size() != vector2.size()) { throw new IllegalArgumentException("The vectors have different dimentions."); } List<Integer> result = new ArrayList<Integer>(); Iterator<Integer> iter1 = vector1.iterator(); Iterator<Integer> iter2 = vector2.iterator(); while (iter1.hasNext()) { result.add(iter1.next() - iter2.next()); } return result; } /** * * @param vector * @param sparseVector * @return */ public static List<Integer> subtract(List<Integer> vector, int[] sparseVector) { List<Integer> result = new ArrayList<Integer>(vector); for (int offset : sparseVector) { if (offset == -1) { continue; } result.set(offset, result.get(offset) - 1); } return result; } /** * The addition of two vectors. * * @param vector1 * @param vector2 * @return * @throws IllegalArgumentException */ public static List<Integer> add(List<Integer> vector1, List<Integer> vector2) { if (vector1.size() != vector2.size()) { throw new IllegalArgumentException("The vectors have different dimentions."); } List<Integer> result = new ArrayList<Integer>(); Iterator<Integer> iter1 = vector1.iterator(); Iterator<Integer> iter2 = vector2.iterator(); while (iter1.hasNext()) { result.add(iter1.next() + iter2.next()); } return result; } /** * The addition of two vectors, one of which is a sparse vector with value * 1. * * @param vector * @param sparseVector * @return */ public static List<Integer> add(List<Integer> vector, int[] sparseVector) { List<Integer> result = new ArrayList<Integer>(vector); for (int offset : sparseVector) { if (offset == -1) { continue; } result.set(offset, result.get(offset) + 1); } return result; } }