Amit Deshpande
Amit Deshpande

Reputation: 19185

How Arrays.asList(int[]) can return List<int[]>?

While doing simple program I noticed this issue.

int[] example = new int[10];
List<Integer> exampleList = Arrays.asList(example);// Compilation error here  

Compilation error is returned as cannot convert from List<int[]> to List<Integer>. But List<int> is not permitted in java so why this kind of compilation error?

I am not questioning about autoboxing here I just wondered how Arrays.asList can return List<int[]>.

asList implementation is

public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}

So it is treating int[] as T that is why this is happening.

Upvotes: 16

Views: 18777

Answers (4)

Marko Topolnik
Marko Topolnik

Reputation: 200158

The signature <T> List<T> asList(T... os) involves a generic type T. The extent of generic types covers only reference types (including array types), but not primitive types. Therefore in an invocation Arrays.asList(ints) with int[] ints the T can only be resolved to int[].

On a higher level, the purpose of Arrays.asList is to provide a list view of your array. That means that the array stays untouched and its elements are made available through an object implementing List. However, Lists can only contain objects and your array contains primitive ints. That makes the array uneligible for being exposed through a List interface.

Anyway, if you are looking for an elegant syntax to create a list of Integers, write Arrays.asList(1,2,3).

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

There is no automatic autoboxing done of the underlying ints in Arrays.asList.

  • int[] is actually an object, not a primitive.

  • Here Arrays.asList(example) returns List<int[]>. List<int> is indeed invalid syntax.

  • You could use:

    List<Integer> exampleList = Arrays.asList(ArrayUtils.toObject(array));

    using Apache Commons ArrayUtils.

Upvotes: 12

Alonso Dominguez
Alonso Dominguez

Reputation: 7858

Arrays.asList(...) works perfectly to transform an array of objects into a list of those objects.

Also, Arrays.asList(...) is defined in Java 5 with a varag construct, or, in order words, it can accept an undefined number of parameters in which case it will consider all of those parameters as members of an array and then return a List instance containing those elements.

List<Object> objList = Arrays.asList(obj1, obj2, obj3);

That means you can create a list with a single element:

List<Object> objList = Arrays.asList(obj1);

Since List<int> is not permitted in Java, when you use a int[] array as parameter for Arrays.asList it will consider it as the single element of a list instead of an array of int. That's because arrays are objects by themselves in Java while an int (and any other primitive) is not.

So, when converting an array of primitives, Arrays.asList will try to return a list of primitive arrays:

List<int[]> intArrayList = Arrays.asList(new int[]{ 1, 2, 3 });

Upvotes: 2

esaj
esaj

Reputation: 16035

Arrays are objects in java, not primitive types. Note that it says List<int[]> (list of int-arrays), not List<int>.

Upvotes: 1

Related Questions