在android中减去2个不同列表中的值

时间:2013-04-04 10:07:16

标签: java android arrays

我有一个整数年的数组及其对应的值,它看起来像下面的代码

int[] earningYear = {2012,2013,2014};
int[] earningAmount = {100,150,120};
int[] expenseYear = {2012,2014};
int[] expenseAmount = {50,30};

我想从与年份相对应的earningsAmount中减去expenseAmount,输出列表应该如下所示

int[] savingsYear = {2012,2013,2014};
int[] savingsamount = {50,150,90};

请帮我这样做。提前致谢

2 个答案:

答案 0 :(得分:1)

如果earningYear总是包含所有年份,并且earningYear和expenseYear都被排序,那么这将是一个可能的解决方案

int[] earningYear = {2012,2013,2014};
int[] earningAmount = {100,150,120};
int[] expenseYear = {2012,2014};
int[] expenseAmount = {50,30};

int[] savingsYear = new int[earningYear.length()];
int[] savingsAmount = new int[earningYear.length()];

int expenseYearIndex = 0;

for(int i = 0; i < earningYear.length(); i++) {
    savingsYear[i] = earningYear[i];
    savingsAmount[i] = earningsAmount[i];
    if(expenseYear.length() > 0 && expenseYear[expenseYearIndex] == earningYear[i]) {
        savingsAmount[i] -= expenseYear[expenseYearIndex];
        expenseYearIndex++;
    }
}

这应该这样做。

然而,更好的解决方案是使用已经建议的List,以及一些持有年份和年份金额的对象。

答案 1 :(得分:1)

@ umair.ali和@Casper给出的答案很好,将阵列转换为地图是可行的方法。
我建议使用TreeMap(或SortMap接口的其他实现)。 TreeMap默认按升序对其键进行排序。这样,如果你真的希望得到明确的节省数组,那么在处理之后你就不必进行任何笨拙的地图排序。

这是完整的例子:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class SavingsApp {

    public Map<Integer, Integer> arraysToMap(int[] years, int[] money) {
        Map<Integer, Integer> newMap = new HashMap<Integer, Integer>();
        if (years == null || money == null || years.length != money.length) {
            throw new IllegalArgumentException();
        }
        for (int i=0; i< years.length; i++ ) {
            newMap.put(years[i], money[i]);
        }

        return newMap;
    }

    public Map<Integer, Integer> calculateSavings(Map<Integer, Integer> earningsMap, Map<Integer, Integer>expensesMap) {
        Map<Integer, Integer> savingsMap = new TreeMap<Integer, Integer>();
        savingsMap.putAll(earningsMap);

        for (Entry<Integer, Integer> expensePerYear : expensesMap.entrySet()) {
            Integer year = expensePerYear.getKey();
            Integer expense = expensePerYear.getValue();

            Integer earning = savingsMap.get(year);
            if (earning == null) {
                earning = 0;
            }
            savingsMap.put(year, earning-expense);
        }
        return savingsMap;
    }


    public static void main(String[] args) {
        int[] earningYear = {2012,2013,2014};
        int[] earningAmount = {100,150,120};
        int[] expenseYear = {2012,2014};
        int[] expenseAmount = {50,30};

        SavingsApp app = new SavingsApp();

        // convert arrays to maps
        Map<Integer, Integer> earningsMap = app.arraysToMap(earningYear, earningAmount);
        Map<Integer, Integer> expensesMap = app.arraysToMap(expenseYear, expenseAmount);

        // compute savings per year
        Map<Integer, Integer> savingsMap = app.calculateSavings(earningsMap, expensesMap);

        // convert result map to array
        List<Integer> savingsList = new ArrayList<Integer>(savingsMap.values());
        Integer[] savingsArray = new Integer[savingsList.size()];
        savingsList.toArray(savingsArray);
    }
}

需要注意的一点是savingsArray将是Integer[]类型而不是int[]