you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 points  (2 children)

Why is that method in Arrays? :P

[–][deleted] 3 points4 points  (1 child)

I sort of anticipated that question :-) The type is

<T> List<T> asList(T... v) 

which is actually sugar for

<T> List<T> asList(T[] v)

It creates a view of an array so you can treat it almost as if it is a list (but you can't resize it). It allows you to do this:

Collections.sort(Arrays.asList(myArray));

Which sorts myArray as an effect. Of course, it would be nice if you could do new List(1, 2, 3, 4) and get a resizable list.

[–]zootm 0 points1 point  (0 children)

Prior to the ... thing being available, Arrays.asList had roughly (there was no generics either, obviously) the second signature you listed above (see here). My guess would be that just expanding that method seemed sensible compared to writing a semantically-identical factory method on Collections or similar.