您的位置:首页 > 编程语言 > Java开发

【java基础】collection接口中toArray()的使用方法

2017-09-07 10:03 417 查看
Collection接口中有两种toArray()方法

Object[] toArray()           Return an Array Containing all of the elements in this collection.

<T> T[] toArray(T[] a)     Return an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

 两种方法从集合转换成数组,但是实现上有不同

第一种用法:

一个ArrayList<String>转换成String型数组

Collection<String> coll = new ArrayList<String>();

String[]  str= coll.toArray();

 

第二种用法:

同样,一个ArrayList<String>转换成String型数组,与上不同的是,这要先申请好数组大小

Collection<String> coll = new ArrayList<String>();

String[] theStrings = new String[ coll.size() ];

Coll.toArray(theStrings );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐