C graphics
C graphics

Reputation: 7458

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? 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?

Upvotes: 3

Views: 91

Answers (1)

NPE
NPE

Reputation: 500367

Using Arrays.asList(ints) as suggested by others won't work (it'll give a list of int[] rather than a list of 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():

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

Upvotes: 5

Related Questions