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

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,用以上注释的代码可以实现,为何用
以上的代码不能实现,请指教,谢谢!
...全文
54 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2004-09-22 04:26
社区公告
暂无公告