List stooges = Arrays.asList("Larry", "Moe", "Curly");
Would it be reasonable to do this?
ArrayList stooges = (ArrayList) Arrays.asList("Larry", "Moe", "Curly");
Computer says no! Why?
Arrays.asList uses a internally reimplements of ArrayList.
private static class ArrayList extends AbstractList
Whatever happened to not reinventing the wheel!

That would not be reasonable because the asList() method does not promise to give you anything else than an implementation of List. You can not assume you will get any particular implementation out of this method.
ReplyDeleteIf you absolutely need an ArrayList, then:
ArrayList stooges = new ArrayList(Arrays.asList("Larry", "Moe", "Curly"));
I fully agree with David. And would not have made the silly assumption if I'd not taken a sneaky peek into the source for asList() and jumped to the wrong conclusion when I saw ArrayList.
ReplyDelete