简体   繁体   English

数组无法在Java / Android中正确插入数据

[英]Array not inserting Data Correctly in Java/Android

Here is a method I have: 这是我有一种方法:

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

There are three records so three loops should be happneing. 有三个记录,因此应该发生三个循环。 totalMonthlyFee is being returned as 90. However, the data is 8.33, 45 and 45. I am trying to get the sum of that (98.33 should be correct but I am getting 90?). totalMonthlyFee的返回值为90。但是,数据为8.33、45和45。我试图获取总和(98.33应该是正确的,但我得到90?)。 Anyone see what is wring here? 有人看到这里发生了什么吗?

On second thought if I understand what you are doing I don't think you need the inner loop at all. 再三考虑,如果我了解您在做什么,我根本就不需要内循环。 Maybe try this... 也许试试这个...

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; 

    }

当您已经将i作为循环的一部分进行递增时,请不要在循环主体中递增i。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM