递归方法打印出来的次数太多了?

时间:2015-10-14 17:59:09

标签: java recursion arraylist output

所以我有一个方法可以递归地对GeometricObjects的一个派对进行排序,并按照它们的区域顺序打印出来。但是,我似乎无法弄清楚这个小问题。我的方法是正确打印数据,但它打印出arraylist 6次而不是一次(它有6个对象),任何想法我做错了什么?谢谢!

private static void recursionSort(ArrayList<GeometricObject> list, int low, int high){
    if (low < high){
        int minIndex = low;
        GeometricObject min = list.get(low); //set min to first object in range
        for (int i = low + 1; i <= high; i++){ //loop through all values other than first
            if (list.get(i).getArea() < min.getArea()){ //check if the value is smaller than current min
                min = list.get(i); //if it is update the min
                minIndex = i; //store min index so we can swap locations later
            }
        }

        Collections.swap(list, low, minIndex); //move the original min to new min's old index (tricky wording)
        list.set(low, min); //set the old lowest to the new lowest using store min value

        recursionSort(list, low + 1, high); //call it again

    }
    System.out.println("Recursion sort:" + list);

}

期望的输出:

Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]

输出I&#39;收到

Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]
Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]
Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]
Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]
Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]
Recursion sort:[GeometricObject [color=blue, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=2.0  Area=12.57  Perimeter=12.57 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=4.0  Area=50.27  Perimeter=25.13 ]
, GeometricObject [color=green, filled=false, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=5.0 Width= 11.0  Area=55.0  Perimeter=32.0 ]
, GeometricObject [color=orange, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=10.0 Width= 6.0  Area=60.0  Perimeter=32.0 ]
, GeometricObject [color=red, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Rectangle [ Height=14.0 Width= 12.0  Area=168.0  Perimeter=52.0 ]
, GeometricObject [color=blue, filled=true, dateOfCreation=Wed Oct 14 12:02:33 EDT 2015]
 Circle [ radius=15.0  Area=706.86  Perimeter=94.25 ]
]

3 个答案:

答案 0 :(得分:1)

您正在递归方法的末尾打印列表。因为每次调用都会发生这种情况,一旦最内层的递归调用结束,每次递归调用都会打印列表。

在进行初始递归调用之后,将打印移到递归方法之外。

答案 1 :(得分:1)

这里的问题是每个级别的递归都会在返回之前打印出来。

要修复,您可以重载方法以进行打印:

private static void recursionSort(ArrayList<GeometricObject> list){
    recursionSort(list, 0, list.size())
    System.out.println("Recursion sort:" + list);        
}

private static void recursionSort(ArrayList<GeometricObject> list, int low, int high){
    if (low < high){
        int minIndex = low;
        GeometricObject min = list.get(low); //set min to first object in range
        for (int i = low + 1; i <= high; i++){ //loop through all values other than first
            if (list.get(i).getArea() < min.getArea()){ //check if the value is smaller than current min
                min = list.get(i); //if it is update the min
                minIndex = i; //store min index so we can swap locations later
            }
        }

        Collections.swap(list, low, minIndex); //move the original min to new min's old index (tricky wording)
        list.set(low, min); //set the old lowest to the new lowest using store min value

        recursionSort(list, low + 1, high); //call it again

    }

}

答案 2 :(得分:0)

您是否尝试从您的函数返回列表?

public static void main (String args []){
    ArrayList<GeometricObject> list = new ArrayList<GeometricObject>();
    System.out.println(recursionSort(list, 0,list.size()-1).toString());
}

private static ArrayList<GeometricObject> recursionSort(ArrayList<GeometricObject> list, int low, int high){
    if (low < high){
        int minIndex = low;
        GeometricObject min = list.get(low); 
        for (int i = low + 1; i <= high; i++){ 
            if (list.get(i).getArea() < min.getArea()){ 
                min = list.get(i); 
                minIndex = i; 
            }
        }

        Collections.swap(list, low, minIndex);
        list.set(low, min);

        recursionSort(list, low + 1, high);

    }
    return list;

}   

}

输出: [0, 1, 1, 9, 10, 12, 100]