简体   繁体   English

如何将两个按字典顺序排列的String ArrayList合并到新的第三个ArrayList中?

[英]How can I merge two lexicographically ordered String ArrayLists into a new third ArrayList?

How can I merge two lexicographically ordered String ArrayLists into a new third ArrayList without modifying the two original lists? 如何在不修改两个原始列表的情况下将两个按字典顺序排列的String ArrayList合并到新的第三个ArrayList中? 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. 如果list1是:1、3、5,而list2是:2、4、7、8、8。则新合并的list3应该是:1、2、3、4、5、7、8、8。我需要创建2个while循环,第一个循环直到遍历整个列表,第二个循环从另一个列表中取出剩余物,并将所有剩余元素放在新列表的末尾3。 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. 一切都应该正确,但我需要帮助使while循环。

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);
        }
    }
}

Why not to use Collections.sort() ? 为什么不使用Collections.sort()

ArrayList<String> list3 = new ArrayList<String>(list1);
list3.addAll(list2);
Collections.sort(list3);

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;
}

I would suggest using Collections.sort () here. 我建议在这里使用Collections.sort () 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. Chide将更短,更易读,并且您将使用内置在排序算法中的高性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM