简体   繁体   English

将一个int数组附加到<Integer>的ArrayList中

[英]appending an array of int to an ArrayList of <Integer>

Is there a shortcut to add (in fact append ) an array of int to an ArrayList? 是否有一个快捷方式将一个int数组添加(实际上是附加)到ArrayList? for the following example 对于以下示例

ArrayList<Integer> list=new ArrayList<Integer>();  
    int[] ints={2,4,5,67,8};  

Or do I have to add the elements of ints one by one to list? 或者我是否必须逐个添加整数元素才能列出?

Using Arrays.asList(ints) as suggested by others won't work (it'll give a list of int[] rather than a list of Integer ). 使用其他人建议的Arrays.asList(ints)将不起作用(它将给出一个int[]列表而不是一个Integer列表)。

The only way I can think of is to add the elements one by one: 我能想到的唯一方法是逐个添加元素:

    for (int val : ints) {
        list.add(val);
    }

If you can turn your int[] into Integer[] , then you can use addAll() : 如果你可以将int[]转换为Integer[] ,那么你可以使用addAll()

    list.addAll(Arrays.asList(ints));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM