Java以递归方式从Arraylists的Arraylist中删除元素

时间:2015-08-06 14:45:49

标签: java recursion

我正在编写一个我必须处理ArrayList<ArrayList<Integer>>的软件。我要做的是删除子列表中的重复项,从最短的一个开始,如果它们存在则从其他子列表中删除这些值,依此类推,因为没有更多的重复项。例如,我的原始列表列表是:

[[4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], 
[26, 27, 28, 29, 30, 31], 
[11, 12, 13, 14], 
[13, 14], [9, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]]

我想要的最终结果如下:

[[4, 5, 6, 7, 8, 15, 16, 17, 18, 19, 20], 
[26, 27, 28, 29, 30, 31], 
[11, 12], 
[13, 14], [9, 22, 23, 24, 25]]

从原始列表中我看到子列表[13,14]是最短的,并且这些值在主列表中不是唯一的,然后我将其从所有其他子列表中删除:

[[4, 5, 6, 7, 8, 9, 11, 12, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31], 
[26, 27, 28, 29, 30, 31], 
[11, 12], 
[13, 14], [9, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]]

现在下一个最短的子列表是[11, 12],然后我从所有其他列表中删除这两个值,依此类推。

我真的不知道如何编写递归代码,任何想法?

编辑:子列表的数量不是恒定的。

2 个答案:

答案 0 :(得分:0)

public class Test6 {

    public static void main(String[] args) throws Exception {

        Integer[] list1 = { 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
        Integer[] list2 = { 26, 27, 28, 29, 30, 31 };
        Integer[] list3 = { 11, 12, 13, 14 };
        Integer[] list4 = { 13, 14 };
        Integer[] list5 = { 9, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };

        ArrayList<ArrayList<Integer>> main = new ArrayList<ArrayList<Integer>>();
        main.add(new ArrayList(Arrays.asList(list1)));
        main.add(new ArrayList(Arrays.asList(list2)));
        main.add(new ArrayList(Arrays.asList(list3)));
        main.add(new ArrayList(Arrays.asList(list4)));
        main.add(new ArrayList(Arrays.asList(list5)));

        for (ArrayList<Integer> list : main) {
            System.out.println(list);
        }

        removeDuplicates(main);

        System.out.println("________________________________________");

        for (ArrayList<Integer> list : main) {
            System.out.println(list);
        }

    }

    private static void removeDuplicates(ArrayList<ArrayList<Integer>> main) {
        // Sort the lists based on their size
        Collections.sort(main, new Comparator<ArrayList<Integer>>() {

            @Override
            public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) {
                return Integer.valueOf(o2.size()).compareTo(Integer.valueOf(o1.size()));
            }
        });

        for (ArrayList<Integer> list1 : main) {
            for (ArrayList<Integer> list2 : main) {
                if (list2 != list1) {
                    removeDuplicateNumbers(list1, list2);
                }
            }
        }
    }

    private static void removeDuplicateNumbers(ArrayList<Integer> list1, ArrayList<Integer> list2) {
        for (Integer number : list2) {
            list1.remove(number);
        }
    }
}

答案 1 :(得分:0)

我在大约30分钟内编写了这个伪代码而没有测试它的能力,所以如果有错误请告诉我。你真的不需要递归,但如果你倾向于做递归,你可以这样做(除了这不是递归的最佳例子)。我们的想法是制作源数组的副本。从该副本中,找到最短的子阵列,并从副本中的其余子阵列中删除所有重复项。最后,将最短的内容放入返回数组中,并使用修改后的临时数组和新更新的返回数组再次调用该函数。

tempArray = actualArray;
returnArray = new Array<Array<int>();
filterList(tempArray, returnArray);

void filterList(<Array<Array<int>> temp, Array<Array<int>> returnArray){
    // If we're done with recursion
    if (temp.isEmpty)
        return;

    // Arbitrarily start with a shortest array. Starting empty would result in staying empty
    Array<int> shortestArray = temp[0];

    // Find our shortest array so we can check list contents.
    for(Array<int> subArray in temp){
        if(subArray.length < shortestArray.length){
            shortestArray = subArray;
        }
    }

    // remove shortest from temp so we can work on the rest of the list.
    temp.remove(shortestArray);

    // Loop through the array and remove all instances of repeated items
    for(i = 0; i < shortestArray.length; i++){
        for(Array<int> subArray in temp){
            if(subArray.contains(shortestArray[i])){
                subArray.remove(shortestArray[i]);
            }
        }
    }

    // Place shortest into return, and then enter recursion.
    returnArray.add(shortestArray);
    filterList(temp, returnArray);
}