2012-05-17 71 views
1

這裏有一個方法,我有:陣列沒有在Java中正確插入數據/ Android的

public double getCombinedAprThisMonth() { 
    Cursor c = database.rawQuery("SELECT * FROM debt;", null); 
    double totalMonthlyFee = 0; 
    double SingleAprRate = 0; 
    double[] storeFees; 
    int rows = c.getCount(); 
    storeFees = new double[c.getCount()]; 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     for (int i = 0; i < rows; i++) { 
      String BalColumn = c.getString(c 
        .getColumnIndex("debt_total")); 
      String AprColumn = c.getString(c.getColumnIndex("apr")); 
      double aprColumn = Double.valueOf(BalColumn); 
      double balColumn = Double.valueOf(AprColumn); 

      SingleAprRate = (((aprColumn/100)/12) * balColumn); 
      storeFees[i++] = SingleAprRate; 

     } 

    } 
    for(double i : storeFees) { 
     totalMonthlyFee += i; 
    } 

    c.close(); 
    return totalMonthlyFee; 
} 

有三個記錄,使三個環路應happneing。 totalMonthlyFee返回爲90.然而,數據是8.33,45和45.我試圖得到的總和(98.33應該是正確的,但我越來越90?)。任何人都可以看到這裏擰什麼?

回答

1

第二個想法,如果我明白你在做什麼,我不認爲你需要內在循環。也許試試這個...

int j = 0; 
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
     String BalColumn = c.getString(c 
       .getColumnIndex("debt_total")); 
     String AprColumn = c.getString(c.getColumnIndex("apr")); 
     double aprColumn = Double.valueOf(BalColumn); 
     double balColumn = Double.valueOf(AprColumn); 

     SingleAprRate = (((aprColumn/100)/12) * balColumn); 
     storeFees[j++] = SingleAprRate; 

    } 
+0

謝謝。那是我需要的。 +1 – KickingLettuce

0

當你已經將它作爲循環的一部分遞增時,不要在循環體中增加i。