Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an int[] array to a List?

I expected this code to display true:

int[] array = {1, 2};
System.out.println(Arrays.asList(array).contains(1));
like image 406
tshepang Avatar asked Dec 01 '10 13:12

tshepang


People also ask

Can we convert array to list?

We can use Arrays. asList() method to convert a Java array to List easily.

Can we convert int array to list in Java?

An array can be converted to an ArrayList using the following methods: Using ArrayList. add() method to manually add the array elements in the ArrayList: This method involves creating a new ArrayList and adding all of the elements of the given array to the newly created ArrayList using add() method.

What does int [] do in Java?

int[] list; creates a variable named list of type int[]. This variable is capable of referring to an array of ints, but initially its value is null (if it is a member variable in a class) or undefined (if it is a local variable in a method).


2 Answers

The method Arrays.asList(T ...) is, when generics are erased and varargs are transformed, actually equal to a method of type Arrays.ofList(Object[]) (which is the, binary equivalent, JDK 1.4 version of the same Method).

An array of primitives is an Object (see also this question), but not an Object[], so the compiler thinks you are using the varargs version and generates an Object array around your int array. You could illustrate what's happening by adding an extra step:

int[] array = {1, 2};
List<int[]> listOfArrays = Arrays.asList(array);
System.out.println(listOfArrays.contains(1));

This compiles and is equivalent to your code. It also obviously returns false.

The compiler translates varargs calls into calls with a single array, so calling a varargs method that expects parameters T ... with parameters T t1, T t2, T t3 is equivalent to calling it with new T[]{t1, t2, t3} but the special case here is that varargs with primitives will be autoboxed before the array is created if the method needs an object array. So the compiler thinks the int array is passed in as a single Object and creates a single element array of type Object[], which it passes to asList().

So here's the above code once again, the way the compiler implements it internally:

int[] array = {1, 2};
// no generics because of type erasure
List listOfArrays = Arrays.asList(new Object[]{array});
System.out.println(listOfArrays.contains(1));

Here are some good and bad ways to call Arrays.asList() with int values:

// These versions use autoboxing (which is potentially evil),
// but they are simple and readable

// ints are boxed to Integers, then wrapped in an Object[]
List<Integer> good1 = Arrays.asList(1,2,3);
// here we create an Integer[] array, and fill it with boxed ints
List<Integer> good2 = Arrays.asList(new Integer[]{1,2,3});

// These versions don't use autoboxing,
// but they are very verbose and not at all readable:

// this is awful, don't use Integer constructors
List<Integer> ugly1 = Arrays.asList(
    new Integer(1),new Integer(2),new Integer(3)
);
// this is slightly better (it uses the cached pool of Integers),
// but it's still much too verbose
List<Integer> ugly2 = Arrays.asList(
    Integer.valueOf(1), Integer.valueOf(2), Integer.valueOf(3)
);

// And these versions produce compile errors:
// compile error, type is List<int[]>
List<Integer> bad1 = Arrays.asList(new int[]{1,2,3});
// compile error, type is List<Object>
List<Integer> bad2 = Arrays.asList(new Object[]{1,2,3});

Reference:

  • Java Tutorial > Classes and Objects > Passing Information to a Method or a Constructor > Varargs
  • Arrays.asList(T ...)

But to actually solve your problem in a simple way:

There are some library solutions in Apache Commons / Lang (see Bozho's answer) and in Google Guava:

  • Ints.contains(int[], int) checks whether an array of ints contains a given int
  • Ints.asList(int ...) creates a List of Integers from an int array
like image 50
Sean Patrick Floyd Avatar answered Oct 08 '22 05:10

Sean Patrick Floyd


The Arrays.asList(array) will result in a singleton list of an int[].

It works as you expect if you change int[] to Integer[]. Don't know if that helps you though.

like image 30
aioobe Avatar answered Oct 08 '22 07:10

aioobe