Android 动态arraylist的问题

Android 动态arraylist的问题,android,dynamic,arraylist,Android,Dynamic,Arraylist,在我的项目中,我使用了动态数组,但输出结果是错误的: 我的代码: for (int i = 0; i < save; i++) { a[i] = (double) db.get_a("loc", i); b[i] = (double) db.get_b("loc", i); for (int j = 0; j < 6; j++) { numbers.add(a[i] + b[i]); } Log.i("LOG", "Array

在我的项目中,我使用了动态数组,但输出结果是错误的: 我的代码:

for (int i = 0; i < save; i++) {
    a[i] = (double) db.get_a("loc", i);
    b[i] = (double) db.get_b("loc", i);
    for (int j = 0; j < 6; j++) {
        numbers.add(a[i] + b[i]);
    }
    Log.i("LOG", "Array Index #" + i + " = " + numbers.get(i));
}
Log.i("LOG", "Array size #" + numbers.size());

看来你想做的并不是你想要的

您提供的代码的工作原理如下

save
似乎是
6
。在(
i
save
)的第一次迭代中,您得到a和b

然后在(
j
6
)迭代中,在
数字中插入6倍的结果

代码可以重写为

for (int i = 0; i < save; i++) {
    a[i] = (double) db.get_a("loc", i);
    b[i] = (double) db.get_b("loc", i);
    double sum = a[i] + b[i];
    for (int j = 0; j < 6; j++) {
        numbers.add(sum); // Inserting the same value 6 times
        int cal_index = i * 6 + j;
         // Printing all the ArrayList positions
        Log.i("LOG", "* Array Index #" + cal_index + " = " + numbers.get(cal_index));
    }
     // printing the first 6 positions
    Log.i("LOG", "Array Index #" + i + " = " + numbers.get(i));
}
Log.i("LOG", "Array size #" + numbers.size());
for(int i=0;i
db.get_a和db.get_b是如何实现的?这不清楚,有什么问题吗。请您详细说明,以便用户在此提供帮助
for (int i = 0; i < save; i++) {
    a[i] = (double) db.get_a("loc", i);
    b[i] = (double) db.get_b("loc", i);
    double sum = a[i] + b[i];
    for (int j = 0; j < 6; j++) {
        numbers.add(sum); // Inserting the same value 6 times
        int cal_index = i * 6 + j;
         // Printing all the ArrayList positions
        Log.i("LOG", "* Array Index #" + cal_index + " = " + numbers.get(cal_index));
    }
     // printing the first 6 positions
    Log.i("LOG", "Array Index #" + i + " = " + numbers.get(i));
}
Log.i("LOG", "Array size #" + numbers.size());