问题
I wrote 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 linked to it and learned that asList
method doesn't Autobox stuffs. I checked the returned type in eclipse javadoc preview:
I couldn't quite understand this return type. int[]
is an object and not a primitive so its fine. I'm sure that I'm not getting List<Integer>
(something which I expected) but I'm not sure how to use the thing which is being returned. My questions are:
- 1. How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
- 2. In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
- 3. What good is this method for primitives if things are so uncertain?
回答1:
There are obviously 3 questions here so lets tackle them one by one:
- How exactly do I expect that list methods will work when I'm expecting an List of Integer and getting a List of int[] ?
Well, List
methods will work exactly as expected, a List<T>
is a list of types T
. Here T
is an int[]
so a List<int[]>
will contains arrays as each element:
[{1, 2}, {3, 4}, {1, 6}]
So get(i)
will return the ith
element. In the case of Arrays.asList
the List
contains a single element, namely the int[]
so:
int[] array = {210,211,212};
List<int[]> list = Arrays.asList(array);
Will be
[{210, 211, 212}]
And so
list.get(0)[0] == 210
In case of Strings the return type is List of String and not List of String[]. What sort of implementation differences are there?
String
is an Object
, not a primitive type. The difference follows from that.
- What good is this method for primitives if things are so uncertain?
Things are not uncertain. This method results in defined and predictable behaviour. It's just not very useful for primitives. This is (yet another) side effect of combining Java's type system with generics.
Note with Java 8 the conversion of an int[]
to a List<Integer>
is very simple:
List<Integer> list = Arrays.stream(array).
boxed().
collect(toList());
回答2:
You are not getting a Lit or a List (which can't be), you're getting a List of arrays of integer.
So your list does not contain 211, it contains an array that then contains 211.
The array is not "unrolled" into the list, it is added "as is" to a 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:
It is because Arrays.asList()
is a variadic generic function. Change this,
int[] array = {210,211,212};
to
Integer[] array = { 210, 211, 212 };
And the output will be true
and true
.
回答4:
Arrays.asList takes objects as params. Since int[] is an object, you get the List. If you want a list of integers you should do Arrays.asList(211,212,213).
回答5:
int[] array = {210,211,212};
Arrays.asList(array)
is equal to
List<int[]> asList = Arrays.asList(array);
Change
int[] array = {210,211,212};
to Integer[] array = {210,211,212};
and it will work.
and its equals to List<Integer> asList = Arrays.asList(array);
来源:https://stackoverflow.com/questions/25890806/java-understanding-arrays-aslistt-array-method-for-primitive-types