Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

java : Understanding Arrays.asList(T...array) method for primitive types

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:

enter image description here

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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are obviously 3 questions here so lets tackle them one by one:

  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[] ?

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.

  1. 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());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...