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

Amit Deshpande :

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.

Reimeus :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Group by for a list of int arrays

can golang function return interface{}{} - how to return a map list

Why can I assign an int to a Short for return type but not in parameter list?

Delete duplicates in a List of int arrays

How to return the length of a list as type Integer instead of Int in Haskell

How can I return a Dictionary<int, object>?

How can I add an item to a list and return a new list

Can I force SoapClient to return arrays as arrays?

How can I print/return values in multi-dimensional arrays?

How can I map over two arrays and return matches as buttons?

How to use Arrays.asList() method to return a list of objects?

How a function that return an Int can be part of if statement?

How can I sort List[Int] objects?

How to return an Integer or int and a list from a method in java?

How can I convert a text file into a list of int arrays

How can I return a list of summary statistics?

How can I return an object with a list inside of it?

How can i print a list of arrays in JAVA?

how can I compare one int element with a list int

How can I concat two dynamic int-arrays in C?

how to return equivalent Arrays

how can an int method return char

How i can sync list and int in multiprocessings?

How to Do GroupBy with List<List<int>> and Return List<List<int>> in C#?

How can I return a combined Int from an [Int] array?

How to return a list of int in Entity Framework Core using FromSqlRaw?

How to type a function that can take and return arrays of strings or arrays of objects?

how can return future list in list of string?

How Can I Multiply Two Arrays Like int[] * int[] in Java?