Java List Value Add add(List vector, int[] sparseVector)

Here you can find the source of add(List vector, int[] sparseVector)

Description

The addition of two vectors, one of which is a sparse vector with value 1.

License

Open Source License

Parameter

Parameter Description
vector a parameter
sparseVector a parameter

Declaration

public static List<Integer> add(List<Integer> vector, int[] sparseVector) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**//  w  w w .  j av  a 2  s. c om
     * 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;
    }
}

Related

  1. add(List list, E element)
  2. add(List result, ClassLoader loader)
  3. add(List list, E element)
  4. add(List list, int index, T item)
  5. add(List list, int index, T item)
  6. add(List list, Object[] array)
  7. add(List data, Object... values)
  8. add(List> target, E value)
  9. add(List a, List b)

  10. HOME | Copyright © www.java2s.com 2016