本文介绍了什么传递给Arrays实例方法toArray(T [] a)方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您有集合的实例,请说:

If you have an instance of a Collection, say something like:

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

然后填充一堆值,这是最好的方式,if

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?

推荐答案

根据,这是最有效的:

String[] addressesArray = addresses.toArray(new String[addresses.size()]);

我相信他们;)

这篇关于什么传递给Arrays实例方法toArray(T [] a)方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 01:26