Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple items at once to ArrayList in Java [duplicate]

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?

like image 903
Lealo Avatar asked Nov 29 '22 00:11

Lealo


2 Answers

Use Collections.addAll:

Collections.addAll(integerArrayList, 1, 2, 3, 4);
like image 198
Zircon Avatar answered Dec 06 '22 10:12

Zircon


Is your List fixed? If yes the following should work.

List<Integer> integerArrayList = Arrays.asList(1, 2, 3);
like image 35
kar Avatar answered Dec 06 '22 09:12

kar