递归方法不会继续前进并保持在无限循环中(Java)
我正在学习递归作为 Java 教程的一部分,并且正在寻求一些帮助。
我们需要编写一个递归 Java 程序,该程序将计算出在没有直飞航班的情况下如何从一个城市到达另一个城市。
我的问题是,我遇到了一个无限循环,似乎只尝试了两个城市,然后代码一次又一次地重复。
如果可以的话,我将不胜感激。
我不想让你们的代码过多,所以我会提供你们应该需要的方法。如果您需要更多,我会很乐意添加更多代码。
public boolean determineRoute(City from, City to, ArrayList<City> flightRoute) {
int i = 0;
ArrayList<City> Connections = new ArrayList<City>();
// the Connections value takes all the connecting cities we can travel to from a departure point
Connections = from.getConnections();
// searches in the connecting cities from the current city as to if it contains the city we wish to travel to
if (Connections.contains(to)) {
System.out.println("Congrats you can go their cause one of its connecting cities is the to city that u wanna go to");
return true;
} else {
// add connecting city to list for future reference
flightRoute.add(Connections.get(i));
System.out.println(Connections.get(i) + " added to flight route");
// saves current connection
City theCity = Connections.get(i);
// recursive part which sends a new from city for analysis until the city we want to travel to arises
determineRoute(from = Connections.get(i), to, flightRoute);
return true;
}
}
I'm learning about recursion as part of a Java tutorial and I am looking for a little help.
We need to make a recursive Java program which will work out how to get from one city to the other when there is no direct flight.
My problem is that Im getting an infinite loop that only seems to try two cities and then the code repeats itself again and again.
I would appreciate some help on this if you could.
I do not want to overflow you guys with code so i'll put up the method that you should need. If you need more I will happily add some more code.
public boolean determineRoute(City from, City to, ArrayList<City> flightRoute) {
int i = 0;
ArrayList<City> Connections = new ArrayList<City>();
// the Connections value takes all the connecting cities we can travel to from a departure point
Connections = from.getConnections();
// searches in the connecting cities from the current city as to if it contains the city we wish to travel to
if (Connections.contains(to)) {
System.out.println("Congrats you can go their cause one of its connecting cities is the to city that u wanna go to");
return true;
} else {
// add connecting city to list for future reference
flightRoute.add(Connections.get(i));
System.out.println(Connections.get(i) + " added to flight route");
// saves current connection
City theCity = Connections.get(i);
// recursive part which sends a new from city for analysis until the city we want to travel to arises
determineRoute(from = Connections.get(i), to, flightRoute);
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是提示:您认为路线上有两次城市合适吗?您目前正在建设此类航线。
Just a hint: Do you think it is appropriate to have a city twice on the route? You are currently building such flight routes.
这段代码没有任何意义。
变量 i 永远不会增加。一座城市的唯一一个连接将被使用。
为什么要在递归调用中进行设置?
为什么您创建一个数组列表只是为了覆盖下一行中对其的唯一引用?
你想要的是Dijkstra算法。
该算法是迭代的,会找到最短的路径长度,这可能是最低的成本、更少的变化或最短的持续时间。
This code makes no sense whatsoever.
Variable i is never incremented. The only one of the connections of a city will ever be used.
Why are you setting from in the recursive call?
Why are you creating an array list simply to overwrite the only reference to it in the next line?
What you want is Dijkstra's algorithm.
This algorithm is iterative and will find the shortest path length, which could be lowest cost, fewer changes or shortest duration.
检测循环并打破它。
你已经用杀死任何递归方法的一件事诅咒了自己:你没有停止条件。
在将城市添加到连接列表之前,请检查该城市是否已出现。如果是,则无需通过递归再次添加。
每次进入方法时,您都会重新创建连接列表,因此每次都会忘记上次调用的值。您可以通过启动调试器并在方法开头添加断点来快速证明这一点。每次输入该方法时,您都会看到连接列表为空。
更好的解决方案是将该列表传递到方法中,以便每次都可以将新值附加到不断增长的列表中。
Detect the cycle and break it.
You have cursed yourself with the one thing that kills any recursive method: you don't have a stopping condition.
Before you add a city to the list of connections, check to see if it already appears. If it does, there's no need to add it again by recursing.
You keep recreating the list of Connections each time you enter the method, so the previous call's values are forgotten each time. You can prove this quickly by firing up a debugger and adding a breakpoint at the start of your method. You'll see that the connections list is empty each time you enter the method.
A better solution would be to pass that list into the method so new values can be appended to the growing list each time.
简而言之:只有当
Connections
包含你想去的城市时,递归方法才会停止递归。由于Connections
中没有添加任何内容,因此如果最初不正确,则这永远不会成立。变量
i
也有同样的问题:它的值永远不会改变,所以同样,不会发生任何不同的情况。In short: the recursive method will stop recursing only when
Connections
contains the city you want to go to. Since nothing is ever added toConnections
, this will never become true, if it's not true initially.You've got sort of the same issue with the variable
i
: its value never changes, so again, nothing different ever happens.