Pablo Pablo - 1 year ago 118
Java Question

How to convert int[] into List<Integer> in Java?

How do I convert

in Java?

Of course, I'm interested in any other answer than doing it in a loop, item by item. But if there's no other answer, I'll pick that one as the best to show the fact that this functionality is not part of Java.

Answer Source

There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>();
for (int index = 0; index < ints.length; index++)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download