【发布时间】:2023-03-19 17:10:01
【问题描述】:
我目前正在尝试填充 Stipulations 类型的对象数组,这是一个
public abstract interface
我填充这个数组的方法如下,popStipAttr 是一个简单的 switch 语句。
public static Stipulations[] popStipArr(ZASAllocation zasAlloc)
{
//ArrayList<String> s = new ArrayList<String>();
ArrayList<Stipulations> stipAL = new ArrayList<Stipulations>();
for(int i = 0; i < NoStip; i++)
{
stipAL.add(popStipAttr(i,zasAlloc));
}
Stipulations[] StipArr = (Stipulations[]) stipAL.toArray();
return StipArr;
}
但是我收到关于投射的错误:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lc.Stipulations;
我在这里到底做错了什么我创建了相同类型的arraylist,为什么将它转换为该类型的数组会抛出这个?
【问题讨论】:
标签: java arrays casting arraylist