Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k 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 (46k 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)

{

    intList.add(i);

}

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...