gpt4 book ai didi

java - ArrayList:java.lang.IndexOutOfBoundsException:索引:283,大小:283

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:42:30 25 4
gpt4 key购买 nike

<分区>

我已经编写了一个算法来计算一趟车辆在轨道上的距离。轨道上的每个点都有一个(纬度,经度)。这是我的代码,我收到上述错误。

List<TrackPoint> tp = new ArrayList<TrackPoint>();    

for (int i = 0; i < tp.size(); i++) {
double lat1 = tp.get(i).getLat();
double lat2 = tp.get(i + 1).getLat();
double lon1 = tp.get(i).getLon();
double lon2 = tp.get(i + 1).getLon();
double radlat1 = Math.PI * lat1 / 180;
double radlat2 = Math.PI * lat2 / 180;
double theta = lon1 - lon2;
double radtheta = Math.PI * theta / 180;
double dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist);
dist = dist * 180 / Math.PI;
dist = dist * 60 * 1.1515;
dist = dist * 1609.344;// distance in miles
double _KM = 1000; //KM the unit of distance
//distance in km
double distInKM = dist / _KM;
DecimalFormat df = new DecimalFormat("###.###");
double dt = Double.parseDouble(df.format(distInKM));
double mileage = 0;
mileage += mileage + dt;
tp.get(i).setMileage(mileage);
}

错误:

java.lang.IndexOutOfBoundsException: Index: 283, Size: 283
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)

此错误指向行:double lat2 = tp.get(i + 1).getLat();我认为该错误与列表大小有关,但我不明白为什么?有人帮我看看我有什么问题。

25 4 0