什么要传递给数组实例方法的toArray(T [] a)方法? [英] What to pass to the Arrays instance method toArray(T[] a) method?
本文介绍了什么要传递给数组实例方法的toArray(T [] a)方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果你有一个集合的实例,这样说:
If you have an instance of a Collection, say something like:
Collection<String> addresses = new ArrayList<String>();
这当中,然后被一帮价值观,这是最好的方式,如果有的话,尽量使用toArray()方法,而不需要一个类型转换填充?
Which were to then be populated with a bunch of values, which is the "best" way, if any, to make use of the toArray() method without requiring a type cast?
String[] addressesArray = addresses.toArray(new String[] {});
String[] addressesArray = addresses.toArray(new String[0]);
String[] addressesArray = addresses.toArray(new String[addresses.size()]);
String[] addressesArray = addresses.toArray(new String[addresses.size() + 5]);
有没有前两者之间的语义差别?是第三个最有效的?是第四个比第三低效率?
Is there any semantic difference between the first two? Is the third most efficient? Is the fourth less efficient than the third?
推荐答案
根据一个FindBugs的报告,这是最有效的:
String[] addressesArray = addresses.toArray(new String[addresses.size()]);
我相信他们;)
这篇关于什么要传递给数组实例方法的toArray(T [] a)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文