Add multiple items to already initialized arraylist in java

前端 未结 6 1710
温柔的废话
温柔的废话 2020-11-27 16:45

I\'m googling it and can\'t seem to find the syntax. My arraylist might be populated differently based on a user setting, so I\'ve initialized it



        
6条回答
  •  旧时难觅i
    2020-11-27 17:09

    Collections.addAll is a varargs method which allows us to add any number of items to a collection in a single statement:

    List list = new ArrayList<>();
    Collections.addAll(list, 1, 2, 3, 4, 5);
    

    It can also be used to add array elements to a collection:

    Integer[] arr = ...;
    Collections.addAll(list, arr);
    

提交回复
热议问题