views:

364

answers:

6

I have a float[] and i would like to get a list with the same elements. I could do the ugly thing of adding them one by one but i wanted to use the Arrays.asList method. There is a problem though. This works:

List<Integer> list = Arrays.asList(1,2,3,4,5);

But this does not.

int[] ints = new int[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);

The asList method accepts a varargs parameter which to the extends of my knowledge is a "shorthand" for an array. So why does the second piece of code returns a List<int[]> but not List<int>.

And is there a way to correct it?

+3  A: 

Because java arrays are objects and Arrays.asList() treats your int array as a single argument in the varargs list.

ChssPly76
That's right, but it's not the real story.
Michael Myers
@mmyers In fact, that's what this question is all about. +1 for ChssPly76
Willi
+3  A: 

How about this?

Integer[] ints = new Integer[] {1,2,3,4,5};
List<Integer> list = Arrays.asList(ints);
JRL
Beat me, but it would be better with an explanation also.
Michael Myers
unbelievable... It works with reference but not with primitive types :D thanks a lot. :)
Savvas Dalkitsis
Hm but is there an easy way to convert an int[] to Integer[]? The thing is i get my array via a method call and i cannot change it.
Savvas Dalkitsis
@Savvas: See http://stackoverflow.com/questions/880581/java-convert-int-to-integer or Jon Skeet's answer here. Libraries like Apache Commons Lang or Guava will be of some help.
Jonik
+11  A: 

There's no such thing as a List<int> in Java - generics don't support primitives.

Autoboxing only happens for a single element, not for arrays of primitives.

As for how to correct it - there are various libraries with oodles of methods for doing things like this, but all of them will basically be going through the array and adding each element to a new list. There's no way round this, and I don't think there's anything to make it easier within the JDK.

(EDIT: I'd been assuming that the starting point of an int[] was non-negotiable. If you can start with an Integer[] then you're well away :)

Just for one example of a helper library, and to plug Guava a bit, there's com.google.common.primitive.Ints.asList.

Jon Skeet
+1  A: 

If you pass an int[] to Arrays.asList(), the list created will be List<int[]>, which is not vaild in java, not the correct List<Integer>.

I think you are expecting Arrays.asList() to auto-box your ints, which as you have seen, it won't.

instanceofTom
+1  A: 

The problem is not with Arrays.asList(). The problem is that you expect autoboxing to work on an array - and it doesn't. In the first case, the compiler autoboxes the individual ints before it looks at what they're used for. In the second case, you first put them into an int array (no autoboxing necessary) and then pass that to Arrays.asList() (not autoboxing possible).

Michael Borgwardt
A: 

It's not possible to convert int[] to Integer[], you have to copy values


int[] tab = new int[]{1, 2, 3, 4, 5};
List<Integer> list = ArraysHelper.asList(tab);

public static List<Integer> asList(int[] a) {
    List<Integer> list = new ArrayList<Integer>();
    for (int i = 0; i < a.length && list.add(a[i]); i++);
    return list;
}
Maciek Kreft