请教一个类型转化的错误。

xhuad 2004-09-22 04:26:35
public customer[] getCustomer(){
/*customer[] customers = new customer[this.cust.size()];
for(int i = 0;i < cust.size(); i++)
{
customers[i] = ((customer)this.cust.elementAt(i));
}
return customers;*/
return ((customer[])cust.toArray());
}
customer是我自己建的一个类,我现在要返回Vector的cust,用以上注释的代码可以实现,为何用
以上的代码不能实现,请指教,谢谢!
...全文
70 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhuad 2004-09-22
  • 打赏
  • 举报
回复
谢谢xunyiren(从jsp开始)
public Object[] toArray(Object[] a)Returns an array containing all of the elements in this Vector in the correct order. The runtime type of the returned array is that of the specified array. If the Vector fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this Vector.
If the Vector fits in the specified array with room to spare (i.e., the array has more elements than the Vector), the element in the array immediately following the end of the Vector is set to null. This is useful in determining the length of the Vector only if the caller knows that the Vector does not contain any null elements.

Specified by:
toArray in interface List
Overrides:
toArray in class AbstractCollection
Parameters:
a - the array into which the elements of the Vector are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the Vector.
Throws:
ArrayStoreException - the runtime type of a is not a supertype of the runtime type of every element in this Vector.
ladofwind 2004-09-22
  • 打赏
  • 举报
回复
gz
xunyiren 2004-09-22
  • 打赏
  • 举报
回复
return (customer[])cust.toArray(new customer[this.cust.size()]);
sduwjr 2004-09-22
  • 打赏
  • 举报
回复
父类不能赋给子类
ETUFO 2004-09-22
  • 打赏
  • 举报
回复
也许: return cust.toArray(new customer[this.cust.size()]);
同样关注你的疑问~
sduwjr 2004-09-22
  • 打赏
  • 举报
回复
类型匹配吗?错误提示是什么样的?
xhuad 2004-09-22
  • 打赏
  • 举报
回复
只能够一个一个取出吗????好麻烦啊!有没有更好的解决方法啊?
jokerjava 2004-09-22
  • 打赏
  • 举报
回复
object[] 不能 转化到customer[]

return ((customer[])cust.toArray());


挨个取出 自己放入customer[]数组
xhuad 2004-09-22
  • 打赏
  • 举报
回复
提示:java.lang.ClassCastException

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧