如何INT []转换成列表<整数GT;在Java中? [英] How to convert int[] into List<Integer> in Java?

查看:153
本文介绍了如何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]);
}

这篇关于如何INT []转换成列表&LT;整数GT;在Java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆