collection.toArray(new String[0])中new String[0]的作用
来源:互联网 发布:最低价进货淘宝代理 编辑:程序博客网 时间:2024/04/30 19:01
new string[0]的作用
比如:String[] result = set.toArray(new String[0]);
Collection的公有方法中,toArray()是比较重要的一个。
但是使用无参数的toArray()有一个缺点,就是转换后的数组类型是Object[]。 虽然Object数组也不是不能用,但当你真的想用一个具体类型的数组,比如String[]时,问题就来了。而把Object[]给cast成String[]还是很麻烦的,需要用到这个:
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
不管是从哪方面看还是一开始就弄成String[]比较好。
具体怎么办呢?其实用带参数的toArray就好了。官方是这样给出的例子:
String[] a = c.toArray(new String[0]);
如果指定的数组能容纳该 collection,则返回包含此 collection 元素的数组。否则,将根据指定数组的运行时类型和此 collection 的大小分配一个新数组。这里给的参数的数组长度是0,因此就会返回包含此 collection 中所有元素的数组,并且返回数组的类型与指定数组的运行时类型相同。
像 toArray 方法一样,此方法充当了基于数组的 API 与基于 collection 的 API 之间的桥梁。更进一步说,此方法允许在输出数组的运行时类型上进行精确控制,并且在某些情况下,可以用来节省分配开销。
假定 l 是只包含字符串的一个已知 List。以下代码用来将该列表转储到一个新分配的 String 数组:
String[] x = (String[]) v.toArray(new String[0]);
注意,toArray(new Object[0]) 和 toArray() 在功能上是相同的。
参数:
a - 存储此 collection 元素的数组(如果其足够大);否则,将为此分配一个具有相同运行时类型的新数组。
Collection Interface Array Operations
The toArray
methods are provided as a bridge between collections and older APIs that expect arrays on input. The array operations allow the contents of a Collection
to be translated into an array. The simple form with no arguments creates a new array of Object
. The more complex form allows the caller to provide an array or to choose the runtime type of the output array.
For example, suppose that c
is a Collection
. The following snippet dumps the contents of c
into a newly allocated array of Object
whose length is identical to the number of elements in c
.
Object[] a = c.toArray();
Suppose that c
is known to contain only strings (perhaps because c
is of type Collection<String>
). The following snippet dumps the contents of c
into a newly allocated array of String
whose length is identical to the number of elements in c
.
String[] a = c.toArray(new String[0]);
- collection.toArray(new String[0])中new String[0]的作用
- collection.toArray(new String[0])中new String[0]的作用
- collection.toArray(new String[0])中new String[0]的作用
- collection.toArray(new String[0])中new String[0]的作用
- collection.toArray(new String[0])中new String[0]的语法解释
- java 中 toArray(new String[0])
- new String[0]的作用
- String[] result = set.toArray(new String[0]);
- String[] result = set.toArray(new String[0]); new String[0]如何理解?
- Collection<String>n=new ArrayList<String>()与List<String>n=new ArrayList()的区别
- new String[] 与new String[] {""}的区别
- String new String(b, 0, len) 理解
- List<String> list1 = new ArrayList<String>(0); List<String> list2 = new ArrayList<String>();不同之处???
- java中String,new String,StringBuffer的思考
- Java中String和new String的区别,比较
- java中String,new String,StringBuffer的思考
- JS中String()与new String()的差异浅析
- new String(str.getBytes("gb2312"),"iso8859_1")的作用
- ArcGIS for JavaScript获取token
- 第一篇博客,写给自己一年的回顾
- redis windows安装
- Android长按图片保存至相册
- Openlayers 判断图形包含点
- collection.toArray(new String[0])中new String[0]的作用
- linux开发环境搭建
- Google 15道古怪面试题(有参考答案)
- win8/10上的应用程序怎样以兼容模式运行?
- Linux的system()和popen()差异
- 文章标题 HDU 1789 : Doing Homework again (贪心)
- Linux(Centos)之安装Nginx及注意事项
- Qt Charts基本组成
- laravel各种路径的获取方法