由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - String[] a = c.toArray(new String[0])
相关主题
库里有一个函数名也叫Time 请问java property file的问题
出个简单题,看你Java APi熟悉到什么程度about builder pattern
高手请进: gabbage collection problemGenerics应该怎么得到它的class?
TIJ上写错了?问一个java基础的初始化的问题,一直搞不明白 (转载)
Re: how to initialize corba object orb in servletRe: strange exception
structure in Java??要随机返回一个Set的里的元素, 如何操作呢?
an Array question帮我了解一下64bit JVM
why doesn't replaceAll work?Re: [转载] create gif/png image in C or ja
相关话题的讨论汇总
话题: string话题: hereiquote话题: new话题: array话题: following
进入Java版参与讨论
1 (共1页)
y********o
发帖数: 2565
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:
g*****g
发帖数: 34805
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]);
:

y********o
发帖数: 2565
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

m******t
发帖数: 2416
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().

o******t
发帖数: 1144
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

m******t
发帖数: 2416
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
1 (共1页)
进入Java版参与讨论
相关主题
Re: [转载] create gif/png image in C or jaRe: how to initialize corba object orb in servlet
java问题:如何match两个正规表达式structure in Java??
Only definition is showed with F3 in Eclipse?an Array question
How to obtain frame id from ActionEvent? Thankswhy doesn't replaceAll work?
库里有一个函数名也叫Time 请问java property file的问题
出个简单题,看你Java APi熟悉到什么程度about builder pattern
高手请进: gabbage collection problemGenerics应该怎么得到它的class?
TIJ上写错了?问一个java基础的初始化的问题,一直搞不明白 (转载)
相关话题的讨论汇总
话题: string话题: hereiquote话题: new话题: array话题: following