HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howjavaintegerintconvertlistinto

Problem

How do I convert int[] into List 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.

Solution

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

int[] ints = {1, 2, 3};
List intList = new ArrayList(ints.length);
for (int i : ints)
{
    intList.add(i);
}

Code Snippets

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
    intList.add(i);
}

Context

Stack Overflow Q#1073919, score: 354

Revisions (0)

No revisions yet.