我在循环中添加,但返回值始终为0,我想不通。
如果我取消注释最后两行之一,它会正确返回手动值,并且课程的其余部分可以工作。ARRAYLIST_SIZE = 10。
public float averageBearing() {
float sumBng = 0;
for (int i = 0; i==ARRAYLIST_SIZE; i++) {
Location l = locList.get(i);
float tempBearing = l.getBearing();
sumBng += tempBearing;
}
float finalBng = sumBng/ARRAYLIST_SIZE;
//remove following lines for real use
//finalBng = (float) (Math.random()*360);
//finalBng = (float) 105.0;
return finalBng;
}
我有理由确定列表中的位置有方位,这是添加方法。我现在必须欺骗轴承,因为该位置只有在我们移动时才具有它,但我在我的固定办公桌前。
public void add(Location location) {
if (locList == null) {
locList = new ArrayList<Location>();
}
//test code to spoof bearing
location.setBearing((float) 105.0);
//use only locations with extra data
if (location.hasBearing() && location.hasSpeed()) {
locList.add(location);
mostRecent = location;
//ensure we have at most 10 recent locations
//no reason to use stale data
while (locList.size()>10) {
locList.remove(0);
}
}
ARRAYLIST_SIZE = locList.size();
}