如何在Android中解决Array IndexOutOfBoundException

时间:2013-04-05 09:41:15

标签: java android arraylist hashmap

我有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 个答案:

答案 0 :(得分:0)

您可以使用以下实用程序方法在没有任何其他数据结构的情况下执行此操作:

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;
    }

输入数组arr可以有任意数量的间隙,输出将是一个对应于连续年份的数组,从firstYear到lastYear。

它要求您首先在所有年份数组中找到最小和最大年份值。

答案 1 :(得分: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);

答案 2 :(得分:0)

  

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

=&GT; 这是一个糟糕的解决方案,因为如果你遵循这个,那么你必须管理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>类型,随时创建收入类型的对象并将其放入ArrayList中。

答案 3 :(得分:0)

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