90

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);?

4
  • 1
    I already answer this question at stackoverflow.com/a/65368082/10304471 Commented Mar 7, 2021 at 6:05
  • @Trishant Saxena: No, not for the general case. More like rhino9's answer. Though the question is underspecified. Commented May 29, 2022 at 9:22
  • Do you want to add the same number hundreds of times? Or are they arbitrary numbers? Commented May 29, 2022 at 9:23
  • This must have been a duplicate in 2013, nearly 5 years after the launch of Stack Overflow. What is the canonical question? Commented May 29, 2022 at 9:39

8 Answers 8

124

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Really, I thought there was a better way? Oh well, looping works. Thx
Yeah, unless you want to create a new list that is initialized with all your values (see my edit above), those are your options.
The second one is definitely the most concise way of doing it
Just what I was looking for
33

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));

1 Comment

This is definitely the most concise way of doing it.
15

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);

Comments

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"); (...);
            }
        };

3 Comments

Still requires multiple calls to add() and adds clutter even if it is compressed to one line
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<Integer> numbersList = new ArrayList<Integer>(java.util.Arrays.asList(theArray)); . (Not tested)
Here's a more detailed answer on why this works.
2

Java 9+ now allows this:

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

The list will be immutable though.

1 Comment

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.
0

In a Kotlin way;

val arList = ArrayList<String>()
arList.addAll(listOf(1,2,3,4,5))

Comments

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);
}

1 Comment

Yes, for some of the examples in answers here. But otherwise not.
-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));

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.