-1

I am studying ArrayList and I know that I can do "arrayList.add(12); arrayList.add(8); ..." to add integers in the array List but are there any way that I can put all the integers(12, 8, 21, 95, 27) with a line of code?

import java.util.ArrayList;

public class ArayList {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        System.out.println();
        arrayList.add(12, 8, 21, 95, 27);
    }
}

1 Answers1

0

Try

arrayList.addAll(Arrays.asList(12, 8, 21, 95, 27));

Similar question Add multiple items to already initialized arraylist in java

dizider
  • 36
  • 2
    Good answer and good find of that other question. I didn't down-vote, but my guess is that it was down-voted because you should have voted to close as duplicate instead. I did that for you. To clear the negative rep the down-vote is giving you, delete the answer. Welcome to StackOverflow and keep up the good work. You'll soon get more rep to be able to do more. – Andreas Apr 14 '20 at 23:30
  • Appreciate for your feedback! – user13316090 May 24 '20 at 14:08