8

So there's Arrays.asList(T... a) but this works on varargs.

What if I already have the array in a T[] a? Is there a convenience method to create a List<T> out of this, or do I have to do it manually as:

static public <T> List<T> arrayAsList(T[] a)
{
   List<T> result = new ArrayList<T>(a.length);
   for (T t : a)
     result.add(t);
   return result;
}
Jason S
  • 184,598
  • 164
  • 608
  • 970

2 Answers2

17

Just because it works with varargs doesn't mean you can't call it normally:

String[] x = { "a", "b", "c" };
List<String> list = Arrays.asList(x);

The only tricky bit is if T is Object, where you should use a cast to tell the compiler whether it should wrap the argument in an array or not:

Object[] x = ...;
List<Object> list = Arrays.asList((Object[]) x);

or

Object[] x = ...;
List<Object[]> list = Arrays.asList((Object) x);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • To clarify further, as I understand, this is because varargs and an array are equivalent to the compiler, so any time you see varargs, you can use an array of the same type. – Gnat Apr 01 '11 at 14:12
  • weird! I always thought varargs functions were only callable in their collect-arguments-as-an-array mode. – Jason S Apr 01 '11 at 14:58
  • @JasonS No, array notation is perfectly swappable with varargs notation. You can even create a main class with `public static void main(String... args){}`. – Timmos Aug 16 '13 at 13:18
0

As you probably already know, there is a Static class called java.util.Collections which has a number of useful methods for dealing wit arrays such as searching and sorting.

As for your question, the Collection interface specifies methods to add, remove and toArray, amongst others. For one reason or another, the API's authors decided that the add and addAll method will be the only input functions provided to the user.

One explanation for why Java Lists cannot add arrays of objects is that Lists use an iterator and iterators are more strict in their scrolling (i.e. going to the next value) than Arrays which do not have to have all their index values i=(1, 2, 5, 9, 22, ...).

Also, Arrays are not type safe; that is, they cannot guarantee that all their elements conform to a specific super-class or interface, whereas generics (of which List is a member) can guarantee type safety. Hence, the list has the chance to validate each item using the add method.

I think that you can rest assure that your method of adding an array to a list is one of the most (if not most) efficient way of achieving this effect in Java.

Sinker
  • 576
  • 1
  • 7
  • 21
  • I'm not sure what question you're answering, but it's not the one I asked. – Jason S Apr 01 '11 at 15:00
  • the first paragraph is irrelevant, the second is background info on the `Collection` and `List` interfaces. Paragraphs 3 and 4 explain why there ** isn't** a more direct way (as opposed to your way) to create a `List` from an array. I hope this helps clarify my answer. – Sinker Apr 02 '11 at 08:35