【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 );
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 );
相关文章推荐
- Java学习之容器上(Collection接口常用方法,Iterator接口,使用foreach循环遍历Collection集合元素,Set集合通用知识(Hashset类,hashcode()与LinkedHashSet类))
- Java中正则表达式使用方法详解-Java基础-Java-编程开发
- 1. 【Java基础】jar, javac, java的具体使用方法
- Java基础---Java中带参数返回值方法的使用(四十)
- Java基础--->09.关于JDK使用旧方法编译出错、警告问题。
- java基础之线程的同步(同步块和同步方法的使用)
- Java基础---Java中无参数无返回值方法使用(三十六)
- JAVA 面向对象 基础概念,匿名对象的使用,方法的重载
- Java基础---Java中无参数带返回值方法的使用(三十七)
- java基础入门-final关键字在类、属性域、方法里面的使用
- Java网络编程基础 Datagram类使用方法
- Java基础---Java中无参数带返回值方法的使用(三十七)
- Java基础问题:static方法中为什么不能使用非static的方法或变量
- 使用java基础反射访问私有域、方法和构造函数
- Java基础---Java中带参数无返回值方法的使用(三十九)
- java基础问题---java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用
- java基础之通过反射获得和使用类的字段、方法、构造器,等
- Java基础:Date和Calendar类的使用方法
- android基础笔记:数据库SQLite应用2(使用java方法)