为什么没有的ArrayList(T [] T)构造函数? [英] Why there is no ArrayList(T[] t) constructor?
问题描述
这是很常见通过以这种方式的对象数组初始化列表:
It is very common to initialize list by array of objects in such way:
Foo[] objs = ...;
ArrayList<Foo> list = new ArrayList<Foo>(Arrays.asList(objs));
我不知道,是没有任何理由的ArrayList desiners不包括数组作为参数的构造函数,这样就可以像初始化:
I wonder, is there any reason why desiners of ArrayList didn't include constructor with array as parameter, so that it could be initialized like that:
ArrayList<Foo> list = new ArrayList<Foo>(objs);
可能是它违反了一些原则,线程安全或者其他什么东西?
May be it violates some principles, thread-safety or something else?
推荐答案
我不知道为什么这不是标准库,但是的番石榴的<一个href=\"http://guava-libraries.google$c$c.com/svn/trunk/javadoc/com/google/common/collect/Lists.html\"><$c$c>Lists$c$c>类有 newArrayList
甚至与类型推断帮助:
I don't know why it's not in the standard library, but Guava's Lists
class has newArrayList
which even helps with type inference:
ArrayList<Foo> list = Lists.newArrayList(objs);
(您可能要导入 Lists.newArrayList
静态,如果你经常使用它。)
(You may want to import Lists.newArrayList
statically if you use it a lot.)
这篇关于为什么没有的ArrayList(T [] T)构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!