Vector 转換成数组的问题

来源:互联网 发布:淘宝店招导航颜色代码 编辑:程序博客网 时间:2024/05/29 02:24

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>导致类型不匹配的错误。