How can I add multiple items at once to an ArrayList?
ArrayList<Integer> integerArrayList = new ArrayList();
Instead of:
integerArrayList.add(1)
integerArrayList.add(2)
integerArrayList.add(3)
integerArrayList.add(4)
...
I would like to: integerArrayList.add(3, 1, 4, 2);
So that I wont have to type so much. Is there a better way to do this?
Use Collections.addAll
:
Collections.addAll(integerArrayList, 1, 2, 3, 4);
Is your List fixed? If yes the following should work.
List<Integer> integerArrayList = Arrays.asList(1, 2, 3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With