Java,将ArrayList转换为数组(toArray)无法进行转换?

时间:2016-09-22 08:27:34

标签: java

有仓库:

public class Depot
{
    private final int x, y;

    public Depot(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

我列出了它:

ArrayList<Depot> depots = new ArrayList<>();
depots.add(new Depot(1, 2));
depots.add(new Depot(5, 7));

并且必须将它们传递给另一种方法:

Object[] d2 = depots.toArray();
op((Depot[]) d2); ****

public void op(Depot[] depots)

但****行触发了异常:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Depot;

1 个答案:

答案 0 :(得分:1)

请尝试以下方法获取与List

相同类型的数组
left: -64px;