我正在使用2个数组,一个是arrayWeight
,另一个是arrayId
。它们都通过List接收数据库中的值。
List<oldDetails> details = olddb.getDetails();
for (oldDetails cn : details) { //Adding weights and id of all time to an array.
double num = cn.getWeight();
int id = cn.getId();
Log.d("num", "equals: " + num);
for (int i = 0; i < count; i++) {
arrayWeight[i] = num;
arrayId[i] = id;
}
}
Log.d("Array", "equals: " + Arrays.toString(arrayWeight));
在LogCat之后,for循环的每一步都是我收到的:
D/num: equals: 70.6
D/num: equals: 60.7
D/num: equals: 8000.0
D/num: equals: 80.6
D/Array: equals: [80.6, 80.6, 80.6, 80.6]
现在我愿意在我的阵列中实现这个目标:
D/num: equals: 70.6
D/num: equals: 60.7
D/num: equals: 8000.0
D/num: equals: 80.6
D/Array: equals: [70.6, 60.6, 8000.0, 80.6]
如何解决数组中只接收列表中的一个值的问题,而不是全部问题?
答案 0 :(得分:2)
您的内部循环将每个值从detail
复制到数组arrayWeigth
和arrayId
中的每个值,因此在最后一次迭代中它会替换每个值。使用单个常规for循环而不是嵌套循环。像,
for (int i = 0; i < details.size(); i++) {
OldDetails cn = details.get(i);
double num = cn.getWeight();
int id = cn.getId();
Log.d("num", "equals: " + num);
arrayWeight[i] = num;
arrayId[i] = id;
}
或更短,
for (int i = 0; i < details.size(); i++) {
OldDetails cn = details.get(i);
arrayWeight[i] = cn.getWeight();
arrayId[i] = cn.getId();
Log.d("num", "equals: " + arrayWeight[i]);
}