0 votes
1 view
in Java by (5.8k points)
How do I convert int[] into List<Integer> 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.

1 Answer

0 votes
by (34.9k points)

There is no alternative for transforming from int[] to List<Integer> as Arrays.asList doesn't trade with boxing and will just generate a List<int[]> which isn't what you require. You have to produce a utility method.

int[] ints = {1, 2, 3};

List<Integer> intList = new ArrayList<Integer>();

for (int i : ints)