88

My arraylist might be populated differently based on a user setting, so I've initialized it with

ArrayList<Integer> arList = new ArrayList<Integer>();

How can I add hundreds of integers without doing it one by one with arList.add(55);?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
batoutofhell
  • 1,279
  • 3
  • 12
  • 17
  • 1
    I already answer this question at https://stackoverflow.com/a/65368082/10304471 – Trishant Saxena Mar 07 '21 at 06:05
  • @Trishant Saxena: No, not for the general case. More like [rhino9's answer](https://stackoverflow.com/questions/15213974/add-multiple-items-to-an-already-initialized-arraylist-in-java/52811238#52811238). Though the question is underspecified. – Peter Mortensen May 29 '22 at 09:22
  • Do you want to add ***the same number*** hundreds of times? Or are they arbitrary numbers? – Peter Mortensen May 29 '22 at 09:23
  • This ***must*** have been a duplicate in 2013, nearly 5 years after the launch of Stack Overflow. What is the canonical question? – Peter Mortensen May 29 '22 at 09:39

8 Answers8

120

If you have another list that contains all the items you would like to add you can do arList.addAll(otherList). Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like

Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
arList.addAll(Arrays.asList(otherList));

or, if you don't want to create that unnecessary array:

arList.addAll(Arrays.asList(1, 2, 3, 4, 5));

Otherwise you will have to have some sort of loop that adds the values to the list individually.

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
scaevity
  • 3,991
  • 13
  • 39
  • 54
32

What is the "source" of those integers? If it is something that you need to hard code in your source code, you may do

arList.addAll(Arrays.asList(1,1,2,3,5,8,13,21));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
14

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

List<Integer> 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);
Radiodef
  • 37,180
  • 14
  • 90
  • 125
5

If you are looking to avoid multiple code lines to save space, maybe this syntax could be useful:

        java.util.ArrayList lisFieldNames = new ArrayList() {
            {
                add("value1"); 
                add("value2");
            }
        };

Removing new lines, you can show it compressed as:

        java.util.ArrayList lisFieldNames = new ArrayList() {
            {
                add("value1"); add("value2"); (...);
            }
        };
  • 1
    Still requires multiple calls to `add()` and adds clutter even if it is compressed to one line –  Aug 20 '17 at 14:11
  • I would have the values in a String Ej. String theValues="1,20,2,1001"; (by hand or loaded from a File). Then pass it to Array: Integer[] theArray = theValues.split(",", -1); Once you have the array, it's easy to pass it to ArrayList: ArrayList numbersList = new ArrayList(java.util.Arrays.asList(theArray)); . (Not tested) – Fran G Aparicio Aug 22 '17 at 07:34
  • Here's a more [detailed answer](https://stackoverflow.com/a/2761018) on why this works. – Alex Sep 10 '19 at 20:20
1

Java 9+ now allows this:

List<Integer> arList = List.of(1,2,3,4,5);

The list will be immutable though.

user197674
  • 748
  • 2
  • 7
  • 22
  • Finally, this is the one line method to add multiple elements to one list. No loops, no additional lists, one simple line of code. Thank you. – XyFreeman Jul 29 '21 at 12:06
0

If you needed to add a lot of integers it'd probably be easiest to use a for loop. For example, adding 28 days to a daysInFebruary array.

ArrayList<Integer> daysInFebruary = new ArrayList<>();

for(int i = 1; i <= 28; i++) {
    daysInFebruary.add(i);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rhino9
  • 51
  • 3
  • 9
0

In a Kotlin way;

val arList = ArrayList<String>()
arList.addAll(listOf(1,2,3,4,5))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-6

I believe scaevity's answer is incorrect. The proper way to initialize with multiple values would be this...

int[] otherList = {1,2,3,4,5};

So the full answer with the proper initialization would look like this

int[] otherList = {1,2,3,4,5};
arList.addAll(Arrays.asList(otherList));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 7
    This does not compile because int is a primitive type and Arrays.asList() does not work correctly on arrays of primitive types. The chosen answer uses Integer, and that is the correct way to do it. – krispy Mar 27 '14 at 19:15