How to concat two ArrayLists?
Solution 1:
You can use .addAll()
to add the elements of the second list to the first:
array1.addAll(array2);
Edit: Based on your clarification above ("i want a single String in the new Arraylist which has both name and number."), you would want to loop through the first list and append the item from the second list to it.
Something like this:
int length = array1.size();
if (length != array2.size()) { // Too many names, or too many numbers
// Fail
}
ArrayList<String> array3 = new ArrayList<String>(length); // Make a new list
for (int i = 0; i < length; i++) { // Loop through every name/phone number combo
array3.add(array1.get(i) + " " + array2.get(i)); // Concat the two, and add it
}
If you put in:
array1 : ["a", "b", "c"]
array2 : ["1", "2", "3"]
You will get:
array3 : ["a 1", "b 2", "c 3"]
Solution 2:
add one ArrayList to second ArrayList as:
Arraylist1.addAll(Arraylist2);
EDIT : if you want to Create new ArrayList from two existing ArrayList then do as:
ArrayList<String> arraylist3=new ArrayList<String>();
arraylist3.addAll(Arraylist1); // add first arraylist
arraylist3.addAll(Arraylist2); // add Second arraylist
Solution 3:
ArrayList<String> resultList = new ArrayList<String>();
resultList.addAll(arrayList1);
resultList.addAll(arrayList2);