ArrayList的IndexOutOfBoundException

时间:2016-07-01 06:50:08

标签: java arraylist initialization runtime-error indexoutofboundsexception

我的代码

public List<GeoPoint> compute_polygon(List<GeoPoint> waypoints, double d)
        {
            int N = waypoints.size();//25
            int T = 2 * N - 1;//49
//          GeoPoint[] reduced_waypoints = waypoints.toArray(new GeoPoint[waypoints.size()]);
            List<GeoPoint> polygon =  new ArrayList<GeoPoint>(T);
            for (int i = 1; i < N - 1; i++)
            {

                GeoPoint M = get_disp(waypoints.get(i - 1), waypoints.get(i), waypoints.get(i + 1), d);
                polygon.add(i, v_add(waypoints.get(i), M));// here it fails and throws exception. 
                polygon.add(T-i, v_sub(waypoints.get(i), M));
            }
}

错误输出

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.set(ArrayList.java:444)
    at de.carbook.vendor.search.PathPolygon.compute_polygon(PathPolygon.java:40)
    at de.carbook.vendor.search.Cleansing_the_waypoints.main(Cleansing_the_waypoints.java:41)

我无法弄清楚导致问题的原因。

错误在polygon.add(i, v_add(waypoints.get(i), M));,此处失败并抛出异常。

我尝试了polygon.add(index,element)polygon.set(index,element)两种情况都失败了

调试时我发现这行List<GeoPoint> polygon = new ArrayList<GeoPoint>(T);初始化为null(空)。这是一个问题吗?

更新1

public List<GeoPoint> compute_polygon(List<GeoPoint> points, double d)
        {
            int N = points.size();//25
            int T = 2 * N - 1;//49
            GeoPoint[] waypoints = points.toArray(new GeoPoint[N]);
            GeoPoint[] polygon = new GeoPoint[T];
            for (int i = 1; i < N - 1; i++)
            {
                GeoPoint M = get_disp(waypoints[i - 1], waypoints[i], waypoints[i + 1], d);
                polygon[i]     = v_add(waypoints[i], M); 
                polygon[T - i] = v_sub(waypoints[i], M);
            }
            GeoPoint A = v_mul(v_nor(v_prp(v_sub(waypoints[1], waypoints[0]))), d);
            GeoPoint B = v_mul(v_nor(v_prp(v_sub(waypoints[N-1], waypoints[N-2]))), d);
            polygon[0] = v_add(waypoints[0], A); polygon[T] = v_sub(waypoints[0], A);
            polygon[N - 1] = v_add(waypoints[N-1], B); polygon[N] = v_sub(waypoints[N-1], B);

            List<GeoPoint> poly =  new ArrayList<GeoPoint>(Arrays.asList(polygon));
            System.out.println(poly);

            return poly;
        }

更新错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49
    at de.carbook.vendor.search.PathPolygon.compute_polygon(PathPolygon.java:67)
    at de.carbook.vendor.search.Cleansing_the_waypoints.main(Cleansing_the_waypoints.java:41)

2 个答案:

答案 0 :(得分:0)

这是因为您试图在特定索引处插入而它不存在。 List<GeoPoint> polygon = new ArrayList<GeoPoint>(T);只是说它应该在内存中占用这么大的数量。如果你没有提到容量,那么随着列表的增长,必须重复重新分配内部数组。

使用此:

polygon.add( v_add(waypoints.get(i), M)) 

答案 1 :(得分:0)

您不应该在新的ArrayList上使用索引插入。内部ArrayList使用基于其长度动态增长的数组。所以请改用add(Object)。