如何将 int[] 转换为 List<Integer>在爪哇? [英] How to convert int[] into List&lt;Integer&gt; in Java?

查看:32
本文介绍了如何将 int[] 转换为 List<Integer>在爪哇?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中如何将int[]转换成List?

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[] 转换为 List as 没有捷径Arrays.asList 不处理装箱,只会创建一个 List 这不是你想要的.你必须制定一个实用方法.

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>(ints.length);
for (int i : ints)
{
    intList.add(i);
}

这篇关于如何将 int[] 转换为 List<Integer>在爪哇?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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