Add elements in ArrayList
- 28th Aug 2018
- 0
- 8810
- arraylist addall method with example different methods in Arraylist to add the elements how to add collection to another collection with example in java how to add collection to list with example in java How to add elements in ArrayList How to add elements in ArrayList with example overloaded add methods in arraylist Various methods in Arraylist to add an elements
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 extends E> c);
4) boolean addAll(int index,Collection extends E> 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 :
- 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);
- }
- }
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 :
- 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);
- }
- }
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 extends E> 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 :
- 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);
- }
- }
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 extends E> c);
This method Inserts all of the elements in the specified collection into the list, starting at the specified position.
Example :
- 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);
- }
- }
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”