0

I'm trying to convert an array of type int to a List by doing

List<Integer> endingRoutesBusStopsList = Arrays.asList(endingRoutesBusStops);

but for some reason I keep getting an error saying

Type mismatch: cannot convert from List<int[]> to List<Integer>

I don't understand what the issue is.

I know doing

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

will solve the error, but then I can't use it the way I want.

Anyone have any ideas?

user1899174
  • 269
  • 1
  • 4
  • 12

3 Answers3

2

This is caused by the fact that int[] is different from Integer[]. Autoboxing does not work on Arrays.

zw324
  • 25,032
  • 15
  • 79
  • 112
2

The issue is because an "int[]" is an Object,

Arrays.asList(T...) gets generic vararg, that it means it treats "int[]" as "Object" (the common superclass for array int[] and Integer is Object)

so that from asList method perspective you don't pass an array of ints, but you pass an object .

In any way you should make implicit convertion from int to wrapper Integer. It is advisable to make it explicitly.

Michal Borek
  • 4,444
  • 2
  • 27
  • 39
0

We are missing some more of your code, but in general, let me try and answer with code:

This works:

Integer[] arrayOfInt = { Integer.valueOf(0), Integer.valueOf(1) };
List<Integer> listOfInt = Arrays.asList(arrayOfInt);

This works too because the primitive "1" is autoboxed to an Integer object:

Integer[] arrayOfInt = { 1, 2, 3, 4 };
List<Integer> listOfInt = Arrays.asList(arrayOfInt);

Finally, this won't work because an int[] cannot be autoboxed to Integer[]:

int[] arrayOfInt = { 1, 2, 3, 4 };
List<Integer> listOfInt = Arrays.asList(arrayOfInt);

UPDATE: this comes from way down in the comments in a discussion with @MichaelBorek . This example repeatedly tries the same code either autoboxing or not. The cost of autoboxing seems to be that the code that uses it takes 5 times longer than the one that uses Objects directly.

Miquel
  • 14,565
  • 7
  • 49
  • 83
  • But be aware that using autoboxing/unboxing is a bad habit. – Michal Borek May 03 '13 at 14:24
  • @MichalBorek I partly agree ;) Check out the first and second example in this answer. What would you rather find in your code? Readability matters, sometimes more than certain performance tweaks (which the compiler is likely to handle sooner or later). That said, beware of things like this NPE: `int n = (new HashMap – Miquel May 03 '13 at 14:37
  • You're right if it comes to this exact example. But I told only that it is a bad habit :) It is (autounboxing/autoboxing) usually overused. Nevertheless as I think your example is perfectly fine for learning :). Consider: int x = otherArrayList.get(1); new ArrayList().add(x). There we have two (un)boxing operations just because of "bad habit" :) – Michal Borek May 03 '13 at 14:42
  • @MichalBorek After reading your last reply, I was basically agreeing with you, but still thinking you were overestimating the cost of autoboxing, so I thought I'd write a quick and dirty test (see [here](http://pastebin.com/M9xURtRH)). Turns out you were very much right. While, arguably, this code is meant to highlight the problem, it's still impressive that the execution time of your example with or without autoboxing takes consistently **5 times longer** (at least on my machine) – Miquel May 03 '13 at 16:08
  • Miquel I'm not sure I understand you properly but in the code you pasted I couldn't see the performance difference. But this one: http://pastebin.com/N6mRhBFM shows the difference – Michal Borek May 03 '13 at 20:20