Java,无法将ArrayList转换为array(toArray)?
有一个停车场:Java,无法将ArrayList转换为array(toArray)?,java,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
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));
但是****行触发了一个异常:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Depot;
请在下面尝试获取与列表相同类型的数组
List<Depot> depts = new ArrayList<Depot>();
depts.add(new Depot(1, 2));
depts.add(new Depot(1, 2));
depts.add(new Depot(1, 2));
Depot[] depots = depts.toArray(new Depot[depts.size()]);
List depts=new ArrayList();
部门增补(新车辆段(1、2));
部门增补(新车辆段(1、2));
部门增补(新车辆段(1、2));
仓库[]仓库=待命部门(新仓库[depts.size()]);
尝试在下面获取与列表相同类型的数组
List<Depot> depts = new ArrayList<Depot>();
depts.add(new Depot(1, 2));
depts.add(new Depot(1, 2));
depts.add(new Depot(1, 2));
Depot[] depots = depts.toArray(new Depot[depts.size()]);
List depts=new ArrayList();
部门增补(新车辆段(1、2));
部门增补(新车辆段(1、2));
部门增补(新车辆段(1、2));
仓库[]仓库=待命部门(新仓库[depts.size()]);