java - Which one is more efficient of using array list? -
which 1 more efficient instantiate list ?
list<type> list = new arraylist<type>(2); list.add(new type("one")); list.add(new type("two"));
or
list<type> list = arrays.aslist(new type("one"), new type("two"));
they create different types of objects. new arraylist<>()
creates java.util.arraylist
, can added to, etc.
arrays.aslist()
uses type happens called arraylist
, nested type (java.util.arrays$arraylist
) , doesn't allow elements added or removed. just wraps array.
now, if don't care differences, end 2 roughly-equivalent implementations, both wrapping array in list<>
interface. surprised see them differ in performance in significant way - ever, if have specific performance concerns, should test them in particular context.
Comments
Post a Comment