我有 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];
谁能帮我做这件事..提前谢谢你