3

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?

1 Answer 1

5

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.