我有一个充满计算的表。每行包含基于个人债务的数字。例如:
第 1 行:Debt 1(名称)、7.9(APR)、1000(余额)、20(MinPayment);
第 2 行:Debt 2(名称)、9.9(APR)、2000(余额)、40(MinPayment);
ETC..
本次测试共有 6 行。我正在用几个数组做几个循环,我得到了上面的错误(在标题中)。
05-17 18:31:47.548: E/AndroidRuntime(2019): 由: java.lang.ArrayIndexOutOfBoundsException: length=6; 索引=6
哪个是奇怪的,因为长度是 6?
这是整个方法:
public int payoffDebt(Double totalDebt) {
Cursor c = database.rawQuery("SELECT * FROM debt;", null);
int monthTotal = 0;
double interestFee = 0;
double interestFeeTotal = 0;
String indBal[] = new String[c.getCount()];
String indPay[] = new String[c.getCount()];
String indApr[] = new String[c.getCount()];
double totalBal[] = new double[c.getCount()];
double totalInterestFees[] = new double[c.getCount()];
int rowCounter[] = new int[c.getCount()];
int j = 0; int k = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
// get individual Bal, APR percent, Payment, store into three arrays;
indBal[k++] = c.getString(c.getColumnIndex("debt_total"));
indPay[k++] = c.getString(c.getColumnIndex("payment"));
indApr[k++] = c.getString(c.getColumnIndex("apr"));
rowCounter[k++] = k;
}
c.close();
while (totalDebt >= 0) {
for (int i : rowCounter) {
interestFee = (((Double.valueOf(indApr[i]) / 100) / 12) * Double
.valueOf(indBal[i]));
totalDebt = totalDebt
- (Double.valueOf(indPay[i]) - interestFee);
interestFeeTotal += interestFee; // sum of all Apr Fees CURRENT
// month in while loop
}
totalBal[j++] = totalDebt; // record total debt for this after all
// payments
totalInterestFees[j++] = interestFeeTotal; // record total interest
// fees for this month
// from all debt
// Increment month
monthTotal += 1;
}
return monthTotal;
问题是第 165 行:
indApr[k++] = c.getString(c.getColumnIndex("apr"));