私は計算でいっぱいのテーブルを持っています。各行には、個人の債務に基づく数値が含まれています。例えば:
行1:債務1(名前)、7.9(APR)、1000(残高)、20(MinPayment);
行2:債務2(名前)、9.9(APR)、2000(残高)、40(MinPayment);
等..
このテストには6つの行があります。いくつかの配列でいくつかのループを実行していますが、上記のエラー(タイトル)が発生しました。
05-17 18:31:47.548:E / AndroidRuntime(2019):原因:java.lang.ArrayIndexOutOfBoundsException:length = 6; index = 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"));