Add elements in ArrayList

We can use add() method of ArrayList to insert new elements to ArrayList

There are 4 overloaded “add” methods defined in ArrayList


1) boolean add(Object o);
2) void add(int index, Object o);
3) boolean addAll(Collection c);
4) boolean addAll(int index,Collection c);

Let’s discuss each of these methods with example

1) boolean add(Object o);

This method inserts the specified element to the end of the list.

Example :

  1. package com.kb.collectionsEx;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class ArrayListAdd {
  5.     public static void main(String[] args) {
  6.     ArrayList<String> list = new ArrayList<String>();
  7.     list.add(“Java”);
  8. list.add(“c++);
  9. list.add(“python”);
  10.     System.out.println(list);
  11.     }
  12. }
package com.kb.collectionsEx;
import java.util.ArrayList;
import java.util.List;
public class ArrayListAdd {
	public static void main(String[] args) {
	ArrayList<String> list = new ArrayList<String>();
	list.add(“Java”);
list.add(“c++”);
list.add(“python”);
	System.out.println(list);
	}
}
Note :
We can see each element is added at the end of the list


2) void add(int index, Object o);

This method inserts the specified element at the specified position in the list.

Example :

  1. package com.kb.collectionsEx;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class ArrayListAddAtIndex {
  5.     public static void main(String[] args) {
  6.     ArrayList<String> list = new ArrayList<String>();
  7.     list.add("Java");
  8. list.add("c++");
  9. list.add("python");
  10. list.add(1,"c");
  11.     System.out.println(list);
  12.     }
  13. }
package com.kb.collectionsEx;
import java.util.ArrayList;
import java.util.List;
public class ArrayListAddAtIndex {
	public static void main(String[] args) {
	ArrayList<String> list = new ArrayList<String>();
	list.add("Java");
list.add("c++");
list.add("python");
list.add(1,"c");
	System.out.println(list);
	}
}
Note :
We can see that element “c” is added at 1st index


3) boolean addAll(Collection c);

This method appends all of the elements in the specified collection to the end of this list, in the same order of specified collection

Example :

  1. Package com.kb.collectionsEx;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class ArrayListAddCollection {
  5.     public static void main(String[] args) {
  6.     ArrayList<String> list1 = new ArrayList<String>();
  7.     list1.add("Java");
  8. list1.add("c++");
  9. ArrayList<String> list2 = new ArrayList<String>();
  10. list2.add("python");
  11. list2.add("c");
  12. list1.addAll(list2);
  13.     System.out.println(list1);
  14.     }
  15. }
Package com.kb.collectionsEx;
import java.util.ArrayList;
import java.util.List;
public class ArrayListAddCollection {
	public static void main(String[] args) {
	ArrayList<String> list1 = new ArrayList<String>();
	list1.add("Java");
list1.add("c++");
ArrayList<String> list2 = new ArrayList<String>();
list2.add("python");
list2.add("c");
list1.addAll(list2);
	System.out.println(list1);
	}
}
Note :
We can see that we have added another collection “list2” to “list1” using “addAll” method and all the elements of “list2” are added at the end of “list1”


4) boolean addAll(int index,Collection c);

This method Inserts all of the elements in the specified collection into the list, starting at the specified position.

Example :

  1. Package com.kb.collectionsEx;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class ArrayListAddCollectionAtIndex {
  5.     public static void main(String[] args) {
  6.     ArrayList<String> list1 = new ArrayList<String>();
  7.     list1.add("Java");
  8. list1.add("c++");
  9. ArrayList<String> list2 = new ArrayList<String>();
  10. list2.add("python");
  11. list2.add("c");
  12. list1.addAll(1,list2);
  13.     System.out.println(list1);
  14.     }
  15. }
Package com.kb.collectionsEx;
import java.util.ArrayList;
import java.util.List;
public class ArrayListAddCollectionAtIndex {
	public static void main(String[] args) {
	ArrayList<String> list1 = new ArrayList<String>();
	list1.add("Java");
list1.add("c++");
ArrayList<String> list2 = new ArrayList<String>();
list2.add("python");
list2.add("c");
list1.addAll(1,list2);
	System.out.println(list1);
	}
}
Note :
We can see that we have added another collection “list2” to “list1” using “addAll” method at specified index “1” and all the elements of “list2” are added at the index “1” of “list1”

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on