Instantiate a new ArrayList:
List<String> myList = new ArrayList<String>();
Iterate over your data structure (with a for loop, for instance, more details on your code would help.) and for each element (yourElement):
myList.add(yourElement);
Answer from pcalcao on Stack Overflow Top answer 1 of 7
51
Instantiate a new ArrayList:
List<String> myList = new ArrayList<String>();
Iterate over your data structure (with a for loop, for instance, more details on your code would help.) and for each element (yourElement):
myList.add(yourElement);
2 of 7
13
If you have an arraylist of String called 'foo', you can easily append (add) it to another ArrayList, 'list', using the following method:
ArrayList<String> list = new ArrayList<String>();
list.addAll(foo);
that way you don't even need to loop through anything.
W3Schools
w3schools.com › java › ref_arraylist_add.asp
Java ArrayList add() Method
Java Data Structures Java Collections Java List Java ArrayList Java LinkedList Java List Sorting Java Set Java HashSet Java TreeSet Java LinkedHashSet Java Map Java HashMap Java TreeMap Java LinkedHashMap Java Iterator Java Algorithms · Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowe
Is there a way to add elements to an Array List all at once?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Add an Array to an ArrayList
I’ve created a REALLY simple code snippet to show what I’m trying to do. I need to add an array that I have defined into an ArrayList. I need an ArrayList as I don’t know how many arrays I will end up with. This will Obvs be in a loop but I just wanted to show the problem I’m having ... More on discourse.processing.org
list.append() vs set.add()
"append" suggests positionality - it's adding something to the end of a list, whereas add just indicates adding to the collection, without neccessarily saying where it's added. Sets don't have an ordering, so there's really no such thing as an "end" of the set to append to, which is likely the rationale behind the different names : add just adds an item, whereas append adds it specifically at the end. More on reddit.com
What is the best way to add items to a list?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
Videos
04:52
Set 4 - Adding objects to an ArrayList - YouTube
04:20
How to Add items to the ArrayList all at Once in Java #java #addall ...
07:11
Java Tutorial Adding Strings to ArrayList Using a While Loop - YouTube
01:00
'add' Method in ArrayLists - YouTube
04:45
Java ArrayList Add Method: Code & Examples - Video | Study.com
11:55
Learn Java Programming - ArrayList add(...) Method Tutorial - YouTube
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
Reddit
reddit.com › r/javahelp › is there a way to add elements to an array list all at once?
r/javahelp on Reddit: Is there a way to add elements to an Array List all at once?
November 2, 2021 -
For example I can do this with a basic integer array:
int[] test1 = new int[]{2, 1, 5, 1, 3, 2};But with an Array list I have to add them one at a time like this
List<Integer> test1 = new ArrayList<Integer>(); test1.add(2); test1.add(1); test1.add(3);
?
Top answer 1 of 4
4
If you know your values before hand you can do List.of(x, y, z,...); Note that this will give you an immutable list
2 of 4
3
There are many ways, here are some I find most elegant: List list1 = new ArrayList<>(List.of(1, 2, 3, 4)); // or List list2 = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
CodeHS
codehs.com › tutorial › 13901
Tutorial: ArrayLists in Java | CodeHS
Learn how to create and use ArrayLists in your programs!
Edureka Community
edureka.co › home › community › categories › java › how can i add new elements to an array in java
How can I add new elements to an Array in Java | Edureka Community
April 19, 2018 - I want to append elements to an array String[] mm= {"5","4","3"}; mm.append("2"); This ... give me a way in which I can add elements to this array.
Processing Foundation
discourse.processing.org › beginners
Add an Array to an ArrayList - Beginners - Processing Community Forum
January 6, 2020 - I’ve created a REALLY simple code snippet to show what I’m trying to do. I need to add an array that I have defined into an ArrayList. I need an ArrayList as I don’t know how many arrays I will end up with. This will Obvs be in a loop but I just wanted to show the problem I’m having When I run the following float[] stuff = {1,2,3}; ArrayList list = new ArrayList (); list.add(stuff); printArray(list); I am hoping to see 1,2,3 in the first entry for the ArrayList. instead I j...
Coderanch
coderanch.com › t › 409854 › java › Adding-Object-ArrayList
Adding Object to ArrayList (Beginning Java forum at Coderanch)
Instead, you need to construct new Movie objects. Something like: ... But doesn't ArrayList.add(Object) append the object to the array?
Carleton University
people.scs.carleton.ca › ~lanthier › teaching › JavaNotes › COMP1405_Ch06_Loops and ArrayLists.pdf pdf
Chapter 6 Loops and ArrayLists What is in this Chapter ?
The ArrayList keeps the objects altogether so that we can pass the list · around now as a single object … just like we can consider a “bag” to be a · single object, although it may contain many other objects. Notice in the above code that we are adding a String, an int, a Person object · and a Truck object. Notice as well that at the top of the program we · imported java.util.ArrayList.
Wlu
bohr.wlu.ca › cp213 › notes › 14-ArrayList.pdf pdf
14-ArrayList.pdf
This last point is less of a problem now that Java provides · automatic boxing and unboxing of primitives · 14-5 · Copyright © 2016 Pearson Inc. All rights reserved. Using the ArrayList Class · • In order to make use of the ArrayList class, it · must first be imported from the package ·
Uwinnipeg
courses.acs.uwinnipeg.ca › 1903-050 › ArrayList.pdf pdf
ArrayList class ACS-1903 1 R McFadyen
used to iterate through all elements · cannot change anything · Example: iterate through an ArrayList · people =new ArrayList () ; // add some names · people . add ("Joe") ; people . add ("Jasper") ; people . add ("Dick") ; people . add ("Abigail") ; for(String s : people) System.out.print(s+ " "); simpler syntax · than for · ArrayList class · ACS-1903 · 6 · R McFadyen · Example · Consider the program DisplayReadme.java from previous chapter ·
Mindprod
mindprod.com › jgloss › arraylist.html
ArrayList : Java Glossary
When you are done removing elements, you have logically removed them, but the RAM (Random Access Memory) they had allocated is still nailed down, ready to use for future growth. If you want to reclaim it, you must use ArrayList. trimToSize(), which allocates a new internal array just the right size and copies everything over from the new one, and discards the old one to be later garbage collected.
The Java Bootcamp
javapro.academy › home › java blog › algorithms › java dsa cheat sheet
Java DSA Cheat Sheet – The Java Bootcamp
January 14, 2026 - ArrayList provides resizable arrays with amortized O(1) append. It's the default List implementation unless you need frequent insertions at arbitrary positions. The backing array grows by 50% when capacity is exceeded, so pre-size with new ArrayList<>(capacity) if you know the final size. package ...
Torontomu
cs.torontomu.ca › ~aferworn › courses › CPS109 › CLASSES › week07 › ch07 › index.html
Horstmann Chapter 7
Both names and friends reference the same array list to which the string "Harry" was added. ArrayList<String> friends = names; friends.add("Harry");
University of Toronto
dgp.toronto.edu › ~karan › courses › 148 › 148 › lectures › week3 › wk3_queues.pdf pdf
QUEUES 1
A Java ArrayList has operations that let it be used like a Queue (Exercise: look up which ones). Why not just use ArrayList all the time? Because we can optimize our Queue implementation(s) for the specific Queue · operations. Question: What operation takes the most time in ArrayQueue? 8 · Some alternative techniques · Don’t bother shifting · • Keep a variable that says where the head is (since it’s going to move ·