1

我有 5 个年份列表和 5 个与年份列表相对应的金额列表。

year_Earnings = [2011,2012,2013];
year_Expense = [2011,2012];
year_Investment = [2013];
year_Returns=[];
year_Savings=[2011,2012,2013];


amount_Earnings = [10,20,7];
amount_Expense = [5,10];
amount_Investment = [5];
amount_Returns=[];
amount_Savings=[5,10,7];            

显然,当我尝试在单个 for 循环中迭代所有列表时,我会得到 ArrayIndexOutOfBoundException。所以我使用下面的代码将所有列表转换为带有键值对的哈希映射

 Map<Double, Double> earningsMap = listToMap(year_Earnings, amount_Earnings);
     Map<Double, Double> expensesMap = listToMap(year_Expense, amount_Expense);
     Map<Double, Double> investmentMap = listToMap(year_Investment, amount_Investment);
     Map<Double, Double> returnsMap = listToMap(year_Returns, amount_Returns);
     Map<Double, Double> savingsMap = listToMap(year_Savings, amount_Savings);



public Map<Double, Double> listToMap(List<Double> years, List<Double> money) {
    Map<Double, Double> newMap = new HashMap<Double, Double>();
    if (years == null || money == null || years.size() != money.size()) {
        throw new IllegalArgumentException();
    }
    for (int i=0; i< years.size(); i++ ) {
        newMap.put(years.get(i), money.get(i));
    }

    return newMap;
}

现在我想要如下列表

year_Earnings = [2011,2012,2013];
year_Expense = [2011,2012,2013];
year_Investment = [2011,2012,2013];
year_Returns=[2011,2012,2013];
year_Savings=[2011,2012,2013];


amount_Earnings = [10,20,7];
amount_Expense = [5,10,0];
amount_Investment = [0,0,5];
amount_Returns=[0,0,0];
amount_Savings=[5,10,7];

谁能帮我做这件事..提前谢谢你

4

4 回答 4

0

You can do it without any additional data structure, using the following utility method:

public static int[] values(int[] arr, int[] years, int firstYear, int lastYear) {
        int[] res = new int[lastYear-firstYear+1];
        int arrPos = 0;
        for(int i = 0; i < res.length; i++) {
            int year = firstYear + i;
            if (arrPos==arr.length || years[arrPos] >  year)
                res[i] = 0;
            else
                res[i] = arr[arrPos++];
        }
        return res;
    }

The input array arr can have any number of gaps, and the output will be an array corresponding to consecutive years, from firstYear to lastYear.

It requires that you first find the minimum and maximum year value in all year arrays.

于 2013-04-05T09:56:18.817 回答
0

我建议您不要使用这种复杂且健壮的架构,而不要使用它。

存储交易类型的枚举

public enum FinancialType {
 EARNING,
 EXPENSE,
 INVESTMENT,
 RETURN,
 SAVING;
}

将存储

public class FinancialOperation {
  private final FinancialType type;
  private final int year;
  private final BigDecimal value;
}

然后将列表转换为结构的 util 方法。

    private List<FinancialOperation> createFinancialOperation(FinancialType type, List<Double> years, List<Double> money) {

     List<FinancialOperation> result = new ArrayList<>();

     for(int i = 0; i < years.size(); i++) {

        Double year = years.get(i);
        Double money = moneys.get(i);

        if(year == null) {
           continue; //or throw
        }

        BigDecimal amount = BigDecimal.ZERO;

        if(money != null) {
          amount = new BigDecimal(money,MathContext.DECIMAL128);
        }

        result.add(new FinancialOperation(type,year.intValue(),amount);
     }

     return result;

}

用法很容易想象。

List<FinancialOperation> earningsList =  createFinancialOperation(FinancialType.EARNING,year_Earnings, amount_Earnings);
List<FinancialOperation> investmentList =  createFinancialOperation(FinancialType.INVESTMENT,year_Investment, amount_Investment);

Map<FinancialType,List<FinancialOperation>> map = new HashMap<>();

  map.put(FinancialType.EARNING,earningsList);
  map.put(FinancialType.INVESTMENT,investmentList);
于 2013-04-05T10:04:59.887 回答
0

我有 5 个年份列表和 5 个与年份列表相对应的金额列表。

=>这是一个糟糕的解决方案,因为如果你遵循这个,那么你必须管理 10 个列表,相反我建议你创建 Only Single ArrayList<Object>,每个项目都是 Object 类型的。

1) 使用 getter/setter 方法定义一类收益。

public class Earning {

    int year;
    int amount;

    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
}

2) 定义ArrayList<Earning>类型,随时创建 Earnings 类型的对象并将其放入 ArrayList 中。

于 2013-04-05T10:12:40.900 回答
0

ArrayIndexOutOfBoundException例外,因为您的列表大小不同...在所有列表中使用相同大小,此错误将消失...

于 2013-04-05T10:16:12.697 回答