How to add elements in ArrayList
In this post we will see various ways to add elements in ArrayList in java.
ArrayList provides following methods to add elements.
1. boolean add(E e)
Adds an 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 19 20 21 22 23 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListAddElementsExample1 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); // use add(E e) method to add elements arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); System.out.println("Printing elements from ArrayList that we added "); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 | Printing elements from ArrayList that we added Java JSP JavaScript |
2. void add(int index, E element)
Adds an element at a specified position in the arrayList
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 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListAddElementsExample2 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); // use add(E e) method to add elements arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); // add a element in the 2nd position using add(int index, E element) arrayList1.add(1, "Spring MVC"); System.out.println("Printing elements from ArrayList that we added "); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 5 | Printing elements from ArrayList that we added Java Spring MVC JSP JavaScript |
3. boolean addAll(Collection<? extends E> c)
Adds all the elements that are specified in the collection 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 19 20 21 22 23 24 25 26 27 28 29 30 31 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListAddElementsExample3 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); // use add(E e) method to add elements arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); // Create another ArrayList List<String> arrayList2 = new ArrayList<>(); arrayList2.add(".Net"); arrayList2.add("C#"); // Now add the new List to old. Elements get added at the end of list 1 arrayList1.addAll(arrayList2); System.out.println("Printing elements from ArrayList that we added "); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 5 6 | Printing elements from ArrayList that we added Java JSP JavaScript .Net C# |
4. boolean addAll(int index, Collection<? extends E> c)
Adds all the elements that are specified in the collection at a specified position in the arrayList.
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 27 28 29 30 31 32 | package com.kscodes.sampleproject; import java.util.ArrayList; import java.util.List; public class ArrayListAddElementsExample4 { public static void main(String[] args) { // Create an ArrayList List<String> arrayList1 = new ArrayList<>(); // use add(E e) method to add elements arrayList1.add("Java"); arrayList1.add("JSP"); arrayList1.add("JavaScript"); // Create another ArrayList List<String> arrayList2 = new ArrayList<>(); arrayList2.add(".Net"); arrayList2.add("C#"); // Now add the new List to old at 2nd position. Elements get added after // the 1st element of List 1 arrayList1.addAll(1, arrayList2); System.out.println("Printing elements from ArrayList that we added "); for (String element : arrayList1) { System.out.println(element); } } } |
Output
1 2 3 4 5 6 | Printing elements from ArrayList that we added Java .Net C# JSP JavaScript |