TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java ArrayList add and addAll (Insert Elements)

Use the add method on ArrayList to add elements at the end. Specify an index to insert elements.
Add. This method appends an element to the end of an ArrayList. Add must be passed an element of the ArrayList's type, like String or Integer.ArrayList
With 2 arguments, we can insert an element with add(). There is no insert method. We must specify a valid insertion index as the first argument to add().
Add example. This program uses add() with one argument, and then with two arguments. We first append strings with values "cat" and "dog" to the end of the ArrayList.

Then: We invoke add() with two arguments. The first argument is 0. This means we want to insert "bird" at the first index.

Java program that uses add, add with index import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create an ArrayList and add two strings. ArrayList<String> list = new ArrayList<>(); list.add("cat"); list.add("dog"); System.out.println(list); // Add a String at index 0. // ... This shifts the following elements to make room. list.add(0, "bird"); System.out.println(list); } } Output [cat, dog] [bird, cat, dog]
Add class instances. Here is an example of adding class instances to an ArrayList. With ArrayList we can specify built-in classes like String or Integer, but custom classes too can be used.

Animal: Here we use an Animal class with two fields (weight and color) and provide an Animal constructor.

Constructors

Add: We add three new Animals to the ArrayList. We create Animal instances pass them to add. Then we print the elements.

Java program that adds class instances to ArrayList import java.util.ArrayList; class Animal { public int weight; public String color; public Animal(int weight, String color) { // Create new Animal instance. this.weight = weight; this.color = color; } } public class Program { public static void main(String[] args) { // Create an ArrayList of custom objects. ArrayList<Animal> list = new ArrayList<>(); // Add new objects to the ArrayList. list.add(new Animal(10, "white")); list.add(new Animal(5, "orange")); list.add(new Animal(8, "grey")); // Display elements in our list. for (Animal animal : list) { System.out.println(animal.getClass().getName() + "/" + animal.weight + "/" + animal.color); } } } Output Animal/10/white Animal/5/orange Animal/8/grey
AddAll example. Let us begin with an ArrayList of String elements. We use the names of pet cats. We add three Strings to the first ArrayList.

Then: We create a second ArrayList with just one element ("Meow"). With System.out.println we display these ArrayLists.

Console

Finally: We invoke addAll to add the second ArrayList at the end of the first ArrayList.

Info: With addAll, we can add an ArrayList at the end of another ArrayList. The elements are copied.

Java program that uses ArrayList addAll import java.util.ArrayList; public class Program { public static void main(String[] args) { // Create ArrayList and add 3 strings to it. ArrayList<String> cats1 = new ArrayList<>(); cats1.add("Max"); cats1.add("Mittens"); cats1.add("Fluffy"); System.out.println(cats1); // Create a second ArrayList. ArrayList<String> cats2 = new ArrayList<>(); cats2.add("Meow"); System.out.println(cats2); // Add first ArrayList to second. cats2.addAll(cats1); System.out.println(cats2); } } Output [Max, Mittens, Fluffy] [Meow] [Meow, Max, Mittens, Fluffy]
AddAll, index. Here we insert one ArrayList's elements into another. We use addAll as an "insert all" or "insert range" method. We use an ArrayList of Integers.ArrayList int, Integer

Result: The contents of the first ArrayList are placed in the second ArrayList. The previous elements are shifted.

Java program that inserts elements with addAll import java.util.ArrayList; public class Program { public static void main(String[] args) { // New ArrayList. ArrayList<Integer> ints = new ArrayList<>(); ints.add(1); ints.add(2); System.out.println(ints); // A second ArrayList. ArrayList<Integer> ints2 = new ArrayList<>(); ints2.add(10); ints2.add(20); System.out.println(ints2); // Insert first ArrayList into second ArrayList. ints2.addAll(1, ints); System.out.println(ints2); } } Output [1, 2] [10, 20] [10, 1, 2, 20]
Collections.addAll. This method allows us to directly add an array to an ArrayList. If you are trying to add an array, use Collections.addAll instead of ArrayList addAll.Collections.addAll
A summary. With add() we have both an "append" and an "insert" method on ArrayList. With an index argument, we can insert anywhere in an ArrayList. No other insert method is present.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf