Java ArrayList add method example shows how to add elements to ArrayList in Java. Java ArrayList add method example also shows how to add elements at the specified index of ArrayList.
Java ArrayList is a part of the Java Collection framework.
How to add elements to Java ArrayList using the add method?
Java ArrayList add
method which can be used to adds elements to the ArrayList object.
1 |
public boolean add(E e) |
This method adds the specified element at the end of the ArrayList.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.ArrayList; public class JavaArrayListAddExample { public static void main(String[] args) { ArrayList<String> aListDays = new ArrayList<>(); /* * Use add method to add elements to ArrayList. */ aListDays.add("Sunday"); aListDays.add("Tuesday"); //print ArrayList aListDays.stream().forEach(System.out::println); } } |
Output
1 2 |
Sunday Tuesday |
Also, check out how to print ArrayList example.
How to add an element at the specified index of the ArrayList?
By default add
method inserts element at the end of the ArrayList. If you want to add element at the specified index of the ArrayList, you can use the overloaded add
method which also takes an index parameter.
1 |
public void add(int index, E element) |
This method inserts the element at the specified index of the ArrayList. It shifts existing element at the specified index and any subsequent elements to the right by adding 1 to their indices.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.ArrayList; public class JavaArrayListAddExample { public static void main(String[] args) { ArrayList<String> aListDays = new ArrayList<>(); aListDays.add("Sunday"); aListDays.add("Tuesday"); System.out.println("Index of Tuesday is: " + aListDays.indexOf("Tuesday")); /* * Use add method with index parameter * to add element at specified index of ArrayList. */ aListDays.add(1, "Monday"); //print ArrayList aListDays.stream().forEach(System.out::println); //it shifts all elements at/after index 1 to the right System.out.println("New index of Tuesday is: " + aListDays.indexOf("Tuesday")); } } |
Output
1 2 3 4 5 |
Index of Tuesday is: 1 Sunday Monday Tuesday New index of Tuesday is: 2 |
Make sure to always specify the type of ArrayList. Consider below given code.
1 2 3 |
ArrayList aListDays = new ArrayList<>(); aListDays.add("Sunday"); aListDays.add(1); |
Do you think it will compile? If your answer is no, you are wrong. It will compile since we did not declare type along with the ArrayList, it will be considered as ArrayList of objects. The value 1 will be autoboxed to Integer and added to ArrayList. The disadvantage is, when you get elements from such an ArrayList, you will have to explicitly cast the values to the appropriate type.
How to add ArrayList to ArrayList?
Just like any other object. See below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.ArrayList; public class JavaArrayListAddExample { public static void main(String[] args) { ArrayList<String> aListDays = new ArrayList<>(); aListDays.add("Sunday"); aListDays.add("Tuesday"); ArrayList<ArrayList<String>> aListWeeks = new ArrayList<>(); //add ArrayList to ArrayList aListWeeks.add(aListDays); for(ArrayList<String> aListWeekDays : aListWeeks) System.out.println(aListWeekDays); } } |
Output
1 |
[Sunday, Tuesday] |
How to add all elements of ArrayList to another ArrayList?
Use addAll
method to add all elements of one ArrayList to another ArrayList.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList<String> aListPrimaryColors = new ArrayList<>(); aListPrimaryColors.add("Red"); aListPrimaryColors.add("Green"); aListPrimaryColors.add("Blue"); ArrayList<String> aListSecondaryColors = new ArrayList<>(); aListSecondaryColors.add("Yellow"); aListSecondaryColors.add("Orange"); aListSecondaryColors.add("Purple"); //Add all elements from aListSecondaryColors to aListPrimaryColors aListPrimaryColors.addAll(aListSecondaryColors); System.out.println(aListPrimaryColors); |
Output
1 |
[Red, Green, Blue, Yellow, Orange, Purple] |
Elements of other ArrayList will be added at the end of the first ArrayList. If you want to add elements at the specified index, you can use the overloaded addAll
method which also accepts an index. Also, check out how to copy elements of one ArrayList to another ArrayList example.
This example is a part of the Java ArrayList tutorial with examples.
Reference: ArrayList JavaDoc (Java 8)