How can I merge two lexicographically ordered String ArrayLists into a new third ArrayList without modifying the two original lists? Duplicates are allowed, for instance. If list1 is: 1, 3, 5 and list2 is: 2, 4, 7, 8, 8. Then the new merged list3 should be: 1, 2, 3, 4, 5, 7, 8, 8. I need to create 2 while loops, the first one keeps going until through a whole list, and the second one takes the remains from the other list and puts all the leftover elements at the end of the new list3. I basically need to compare both list indexes to see which element advances. Everything should be correct but I need help making the while loops.
Sorry for the long description, but I will up-vote anyone that helps! Thank you!
public class Merge{
public static boolean isStringArrayListSorted(ArrayList<String> data) {
for (int i = 1; i < data.size(); i++) {
if (data.get(i - 1).compareTo(data.get(i)) > 0) {
return false;
}
}
return true;
}
public Merge (ArrayList<String> list1, ArrayList<String> list2){
ArrayList<String> list3 = new ArrayList<String>();
list1.add(0, "a");
list1.add(1, "c");
list1.add(2, "e");
list1.add(3, "g");
list2.add(0, "b");
list2.add(1, "d");
list2.add(2, "f");
list2.add(3, "h");
list2.add(4, "i");
list2.add(5, "j");
while(list1!= null){
if(list1.get(0)> list2.get(0))
{
list3.add(index, element);
}
}
}