Vector 转換成数组的问题

http://blog.csdn.net/treaturebeauty/archive/2005/03/18/323121.aspx

http://topic.csdn.net/t/20020322/09/592486.html

 

Vector.toArray();  
  将Vector对象所有元素转换为一个数组输出。  
   
  Vector.toArray(Object   []);  
  将Vector对象中所有元素转换为一个数组输出,数组的类型就是参数数组的类型。如果Vector对象中元素类型与参数数组的类型一致,就直接输出;如 果Vector对象中元素类型与参数数组的类型<B>不一致</B>,就参照参数数组的类型新建一个数组输出,此新建数组类型与 参数数组的类型一致,大小就是Vector的元素个数。  
   
  后一种形式的解释原文如下: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.  
   
  结论:  
  (Car[])   cars.toArray();      
  如果原来cars对象中元素类型不是Car,则在强制转换时会导致类型不匹配的错误。  
   
  (Car[])   cars.toArray(new   Car[0]);  
  确保cars对象转换出来的数组就是Car类型,在转换时<B>不会</B>导致类型不匹配的错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值