Java: understanding the Arrays.asList (T ... array) method for primitive types

I wrote the following code and was surprised to see the output:

Integer a = 211; int b = 211; int[] array = {210,211,212}; System.out.println(Arrays.asList(array).contains(a)); System.out.println(Arrays.asList(array).contains(b)); 

Output:

 false false 

I found this question and some other questions related to it and found out that the asList method asList not an Autobox product. I checked the returned type in jcladse eclipse preview:

enter image description here

I could not understand this type of return. int[] is an object, not a primitive, so its fine. I am sure that I am not getting a List<Integer> (something that I was expecting), but I'm not sure how to use the thing that returns. My questions:

  • 1. How exactly do I expect list methods to work when I expect an Integer list and get List of int []?
  • 2. In the case of strings, the return type is List of String, not List of String []. What are the differences in implementation?
    • 3. What good is this method for primitives if things are so vague?
+7
java arrays list autoboxing
source share
5 answers

There are obviously 3 questions here, so they can solve them one at a time:

  • How exactly do I expect list methods to work when I expect an Integer list and get an int [] list?

Well, List methods will work exactly as expected, List<T> is a list of T types. Here T is int[] , so a List<int[]> will contain arrays like every element:

 [{1, 2}, {3, 4}, {1, 6}] 

So get(i) will return the ith element. In the case of Arrays.asList List contains one element, namely int[] like this:

 int[] array = {210,211,212}; List<int[]> list = Arrays.asList(array); 

Will be

 [{210, 211, 212}] 

So,

 list.get(0)[0] == 210 

In the case of strings, the return type is List of String, not List of String []. What are the differences in implementation?

String is an Object , not a primitive type. Hence the difference.

  1. What good is this method for primitives if everything is so vague?

Things are not vague. This method leads to specific and predictable behavior. This is just not very useful for primitives. This is (another) side effect of combining a system like Java with generics.

Note that with Java 8, converting int[] to List<Integer> very simple:

 List<Integer> list = Arrays.stream(array). boxed(). collect(toList()); 
+5
source share

You do not get Lit or List (which cannot be), you get a list of arrays of integers.

So your list does not contain 211, it contains an array that contains 211.

The array is not “expanded” to the list; it is added “as is” to the newly created list.

So:

 System.out.println(Arrays.asList(array).contains(array)); // Will return true System.out.println(Arrays.asList(a).contains(a)); // Will return true 
+3
source share

This is because Arrays.asList() is a variadic generic function. Change it

 int[] array = {210,211,212}; 

to

 Integer[] array = { 210, 211, 212 }; 

And the output will be true and true .

+3
source share

Arrays.asList takes objects as parameters. Since int [] is an object, you get a list. If you need a list of integers, you should do Arrays.asList (211,212,213).

+3
source share
 int[] array = {210,211,212}; Arrays.asList(array) 

equally

 List<int[]> asList = Arrays.asList(array); 

Change

int[] array = {210,211,212}; to Integer[] array = {210,211,212}; and it will work. and its value is List<Integer> asList = Arrays.asList(array);

+2
source share

All Articles