gpt4 book ai didi

java - 递归方法打印太多次?

转载 作者:行者123 更新时间:2023-11-30 03:13:38 26 4
gpt4 key购买 nike

所以我有一个方法,可以递归地对 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 ]
]

我收到的输出

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 ]
]

最佳答案

这里的问题是每一级递归都会在返回之前打印出列表。

要解决此问题,您可以重载打印方法:

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

}

}

关于java - 递归方法打印太多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33132407/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com