Redian新闻
>
String[] a = c.toArray(new String[0])
avatar
String[] a = c.toArray(new String[0])# Java - 爪哇娇娃
y*o
1
At the very bottom of the following page:
http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.ht
ml

Suppose that c is known to contain only strings (perhaps because c is of typ
e Collection). The following snippet dumps the contents of c into a
newly allocated array of String whose length is identical to the number of e
lements in c.
String[] a = c.toArray(new String[0]);

The API doc for public T[] toArray(T[] a) says:
avatar
g*g
2
don't make much difference, new String[5] is an array too.

typ
a
e

【在 y********o 的大作中提到】
: At the very bottom of the following page:
: http://java.sun.com/docs/books/tutorial/collections/interfaces/collection.ht
: ml
:
: Suppose that c is known to contain only strings (perhaps because c is of typ
: e Collection). The following snippet dumps the contents of c into a
: newly allocated array of String whose length is identical to the number of e
: lements in c.
: String[] a = c.toArray(new String[0]);
:

avatar
y*o
3
Thx, I was just curious about the capacity of the array at initialization.
It makes no difference if a.length <= c.size(), otherwise, you get null for
a[i] where i>c.size().

【在 g*****g 的大作中提到】
: don't make much difference, new String[5] is an array too.
:
: typ
: a
: e

avatar
m*t
4
String[] a = c.toArray(new String[c.size()]);
is what I usually do.


for

【在 y********o 的大作中提到】
: Thx, I was just curious about the capacity of the array at initialization.
: It makes no difference if a.length <= c.size(), otherwise, you get null for
: a[i] where i>c.size().

avatar
o*t
5
but it is runtime too, right? I guess this String[0] is just a hint on type

【在 m******t 的大作中提到】
: String[] a = c.toArray(new String[c.size()]);
: is what I usually do.
:
:
: for

avatar
m*t
6

type
The idea is to make an array big enough that can be used for both a hint on
type and the actual container returned.

【在 o******t 的大作中提到】
: but it is runtime too, right? I guess this String[0] is just a hint on type
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。