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

时间:2017-05-17 04:13:01

标签: java algorithm list arraylist

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

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(); 我认为错误与列表大小有关,但我无法弄清楚为什么?有人帮我看看我有什么不对。

5 个答案:

答案 0 :(得分:2)

当您转到循环中的最后一个对象(TrackPoint)时,i + 1元素不存在。

double lat2 = tp.get(i + 1).getLat();

答案 1 :(得分:1)

问题出在这里

tp.get(i + 1)

i等于tp.size-1时,您将其等于tp.size()。要解决它,请将循环更改为:

for (int i = 0; i < tp.size() - 1; i++) {

答案 2 :(得分:0)

当你转到循环中的最后一个对象(TrackPoint)时,第(i + 1)个元素不存在。希望以下代码可以。

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

for (int i = 0; i < tp.size()-1; 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);                       
}

答案 3 :(得分:0)

循环迭代直到列表大小。但是在最后一次迭代中,你试图得到(i + 1)你的情况下不存在的索引。

double lat2 = tp.get(i + 1).getLat();

纠正这个.get(i + 1)。

答案 4 :(得分:0)

以上所有答案都是正确的。或者可以尝试使用以下代码

List<TrackPoint> tp = new ArrayList<TrackPoint>();
    for (int i = 0; i < tp.size(); i++) {
    double lat1 = tp.get(i - 1).getLat();                        
    double lat2 = tp.get(i).getLat();
    double lon1 = tp.get(i - 1).getLon();                       
    double lon2 = tp.get(i).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 - 1).setMileage(mileage);
}