32

I have seen the question: Create ArrayList from array

However when I try that solution with following code, it doesn't quite work in all the cases:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ToArrayList {

    public static void main(String[] args) {
        // this works
        String[] elements = new String[] { "Ryan", "Julie", "Bob" };
        List<String> list = new ArrayList<String>(Arrays.asList(elements));
        System.out.println(list);

        // this works
        List<Integer> intList = null;
        intList = Arrays.asList(3, 5);
        System.out.println(intList);

        int[] intArray = new int[] { 0, 1 };
        // this doesn't work!
        intList = new ArrayList<Integer>(Arrays.asList(intArray));
        System.out.println(intList);
    }
}

What am I doing wrong here? Shouldn't the code intList = new ArrayList<Integer>(Arrays.asList(intArray)); compile just fine?

2
  • Thanks for all the answers. I found several reasons for this compilation errors: 1. Java doesn't autobox primitive arrays. 2. Varargs are complicated to correctly use - stackoverflow.com/questions/2925153/…
    – tuxdna
    Commented Jul 8, 2013 at 7:32
  • +1 this is a good question. I recommend anyone reading it to read all the answers, they all add important info.
    – DaveM
    Commented May 27, 2015 at 16:59

6 Answers 6

36

The problem in

intList = new ArrayList<Integer>(Arrays.asList(intArray));

is that int[] is considered as a single Object instance since a primitive array extends from Object. This would work if you have Integer[] instead of int[] since now you're sending an array of Object.

Integer[] intArray = new Integer[] { 0, 1 };
//now you're sending a Object array
intList = new ArrayList<Integer>(Arrays.asList(intArray));

From your comment: if you want to still use an int[] (or another primitive type array) as main data, then you need to create an additional array with the wrapper class. For this example:

int[] intArray = new int[] { 0, 1 };
Integer[] integerArray = new Integer[intArray.length];
int i = 0;
for(int intValue : intArray) {
    integerArray[i++] = intValue;
}
intList = new ArrayList<Integer>(Arrays.asList(integerArray));

But since you're already using a for loop, I wouldn't mind using a temp wrapper class array, just add your items directly into the list:

int[] intArray = new int[] { 0, 1 };
intList = new ArrayList<Integer>();
for(int intValue : intArray) {
    intList.add(intValue);
}
1
  • Thanks for your answer. How can I convert a primitive array to boxed array instead of creating a new one ( ex. not using a loop )? I am only looking forward to a compact piece of code that I can use.
    – tuxdna
    Commented Jul 8, 2013 at 7:33
19

With Java 8 you can do it in one line:

        
int[] intArr = { 1, 1, 2, 3, 5, 8, 11};  
List<Integer> list = Arrays.stream(intArr).boxed().collect(Collectors.toList());  
list.forEach(System.out::println);
1
  • In versions of Java 16 or later, this can be simplified to: Arrays.stream(intArr).boxed().toList(). Commented Jul 16 at 14:52
11

The issue is about Autoboxing.

Java automatically converts int to Integer automatically. But Java cannot convert int[] to Integer[]. Hence the reason.

public static <T> List<T> asList(T... a)

asList is defined as above. It expects a vararg of type T. i.e. it can take an array of objects of type T. In your case because Java cannot convert int[] to Integer[], hence it takes type T as int[] rather than Integer as desired. Hence you get a list of type List<int[]. You will have to manually convert it into Integer[] from int[].

There is an excellent item in the book Effective Java by Joshua Bloch where he explains the pitfalls of varargs. This is one of them.

1
  • 1
    Great Answer. Thanks. Commented Jun 12, 2021 at 5:15
7

It doesn't work because you're using an int[] instead of Integer[]. Try this:

Integer[] intArray = new Integer[] { 0, 1 };
// this should work now
intList = new ArrayList<Integer>(Arrays.asList(intArray));
3

Collection wants Objects and primitives don't derive from Object. so Integer allow but int is not allow.

So you have to use wrapper class Integer instead of int which is primitive type.

Just example:

 Integer[] intArray = new Integer[] { 0, 1 };
 intList = new ArrayList<Integer>(Arrays.asList(intArray));
1

The below provides an example:

import java.util.ArrayList;
import java.util.Arrays;

public class MyArrays {
    public static void main(String[] args) {

        int i, j = 0;

        Integer[] StandArr = new Integer[10];
        for (i = 0; i < StandArr.length; i++) {
            StandArr[i] = i * 3;
        }

        for (j = 0; j < StandArr.length; j++) {
            System.out.print(StandArr[j] + " ");
        }
        System.out.println();

        ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(StandArr));
        System.out.println(arr);
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.