如何INT []转换成列表<整数GT;在Java中?
问题描述:
如何转换 INT []
到列表<整数GT;
中的Java
How do I convert int[]
into List<Integer>
in Java?
当然,我感兴趣的任何其他答案不是项目做一个循环中,项。但是,如果没有其他的答案,我会挑选一个作为最佳显示的事实,这个功能不是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.
答
有没有捷径从 INT []
转换为列表&LT;整数GT ;
为 Arrays.asList
不涉及拳击,只是创建一个列表&LT; INT []&GT;
这是不是你想要的。你必须做出一个实用的方法。
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++)
{
intList.add(ints[index]);
}