1

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);
        }
    }
}
MercedezB
  • 53
  • 1
  • 7
  • fyi, you don't need the indices in your list add() calls, the single argument add() methods appends to the end. – Roman Apr 14 '16 at 03:55

3 Answers3

2

Why not to use Collections.sort()?

ArrayList<String> list3 = new ArrayList<String>(list1);
list3.addAll(list2);
Collections.sort(list3);
Nurlan
  • 720
  • 7
  • 12
  • Well I would like to create an algorithm to do this with while loops by comparing indexes to determine which element advances into the new list. – MercedezB Apr 14 '16 at 03:57
  • Nowadays CPUs are very fast, so an O(n log n) sort may not worth to concern in compare to an O(n) merge [with the assumption that there's no cost of obtaining the initial sorted list]. So... ^_^ – Ken Cheung Apr 15 '16 at 02:11
2

Assume you really want to do a merge rather than combine the two and sort again, its a simple merge code like this:

public static ArrayList<String> merge(ArrayList<String> L1, ArrayList<String> L2) {
    int i1 = 0, i2 = 0;
    ArrayList<String> result = new ArrayList<String>();

    while (i1 < L1.size() && i2 < L2.size()) {
        if (L1.get(i1).compareTo(L2.get(i2)) < 0) {
            result.add(L1.get(i1));
            i1++;
        } else {
            result.add(L2.get(i2));
            i2++;
        }
    }

    while (i1 < L1.size()) {
        result.add(L1.get(i1));
        i1++;
    }

    while (i2 < L2.size()) {
        result.add(L2.get(i2));
        i2++;
    }

    return result;
}
Ken Cheung
  • 1,778
  • 14
  • 13
  • ArrayList which implements AbstractList allows random access to the elements in the middle while LinkedList implements AbstractSequentialList (which extends AbstractList) only impose an additional "sequential access" only. In my code I didn't manipulate entries in the middle, thus the ArrayList will still preserve the order. – Ken Cheung Apr 14 '16 at 04:13
1

I would suggest using Collections.sort () here. Just create a new list, add all elements and sort it. The chide would be shorter, more readable and you will be using the high preformance built in sorting algorithm.

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28