簡體   English   中英

Java以遞歸方式從Arraylist的Arraylist中刪除元素

[英]Java recursively remove elements from Arraylist of Arraylists

我正在編寫一個必須處理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] ,然后從所有其他列表中刪除這兩個值,依此類推。

我真的不知道如何編寫遞歸代碼,有什么想法嗎?

編輯:子列表的數量不是恆定的。

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

我在大約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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM