将列表的子部分转换为数组时获取非法参数异常

时间:2016-01-10 18:40:40

标签: java arrays list arraylist illegalargumentexception

我有一个列表linePoints,它是Point s。

的列表
private List<Point> linePoints = new ArrayList<>();

我还有一个名为多边形的Point []列表。

private List<Point[]> polygons = new ArrayList<>();

我有一个方法应该做一些事情,然后将linePoints的一部分转换为Point[],这应该被添加到多边形。

这是相关代码:

   //"p" is a point and "i" is a integer
   //The code is supposed to take part of linePoints and turn it into a Point[] in polygons.
   List tempList;
   tempList = linePoints.subList(linePoints.indexOf(p),i); //The problem is on this line
                                                          // I get a "IllegalArgumentException"
    polygons.add((Point[]) tempList.toArray());

为什么我会收到非法参数例外?

编辑1:我做了一些测试,发现我是否更换了

            tempList = linePoints.subList(linePoints.indexOf(p),i);

tempList = linePoints.subList(0,1);

它会起作用。

编辑2:我找到了解决方案。结果是该行的第一个参数大于第二个参数。

现在我还有另外一个问题:

polygons.add((Point[]) tempList.toArray());

我得到的异常是我无法转换

返回的Object []数组
tempList.toArray()

进入Point []。

1 个答案:

答案 0 :(得分:-2)

试试这个:

private List<Point> linePoints = new ArrayList<Point>();
private List<Point[]> polygons = new ArrayList<Point[]>();