Java运行递归程序时出现错误越界异常
我正在学习递归作为 Java 教程的一部分,并且正在寻求一些帮助。
我们需要编写一个递归 Java 程序,该程序将计算出在没有直飞航班的情况下如何从一个城市到达另一个城市。
我的最新问题是,在代码的 FlightRoute 数组列表中有 2 个城市后,我收到错误越界异常。它给出错误“IndexOutOfBoundsException Index 2 Size 2”
连接值是一个数组列表,它包含与该城市连接的所有城市,航班路线也是一个数组列表,它跟踪我们必须前往才能到达的城市我们的目的地。
我只是不明白为什么它不会继续进行。
如果可以的话,我将不胜感激。
我不想让你们的代码过多,所以我会提供你们应该需要的方法。如果您需要更多,我会很乐意添加更多代码。
public boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
{
//the Connections value takes all the connecting cities we can travel to from a departure point
Connections = from.getConnections();
City theCity = Connections.get(i);
//searches in the connecting cities from the current city as to if it contains the city we wish to travel to
if (flightRoute.contains(to)|| 7 >8)
{
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;
}
System.out.println("the City name "+theCity);
if(flightRoute.contains(theCity))
{
System.out.println("Sorry it cannot be added "+Connections.get(i));
}
else
{
//add connecting city to list for future reference
flightRoute.add(Connections.get(i));
//takes the lates connection and uses it for recursion (below)
from = Connections.get(i);
i++;
//recursive part which sends a new from city for analysis until the city we want to travel to arises
determineRoute(from, 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 latest issue is Im getting an error out of bounds exception after the code has 2 cities in the flightRoute array List. it gives the error "IndexOutOfBoundsException Index 2 Size 2"
The connection value is an arrayList which takes all the cities that the city connects with and the flightRoute is also an arrayList which keeps track of the cities we have had to travel to in order to reach our destination.
I just cannot work out why it will not proceed.
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)
{
//the Connections value takes all the connecting cities we can travel to from a departure point
Connections = from.getConnections();
City theCity = Connections.get(i);
//searches in the connecting cities from the current city as to if it contains the city we wish to travel to
if (flightRoute.contains(to)|| 7 >8)
{
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;
}
System.out.println("the City name "+theCity);
if(flightRoute.contains(theCity))
{
System.out.println("Sorry it cannot be added "+Connections.get(i));
}
else
{
//add connecting city to list for future reference
flightRoute.add(Connections.get(i));
//takes the lates connection and uses it for recursion (below)
from = Connections.get(i);
i++;
//recursive part which sends a new from city for analysis until the city we want to travel to arises
determineRoute(from, to, flightRoute);
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在哪里设置
i
值?不管怎样,唯一使用i
作为索引的get()
,所以很明显Connections
没有像i
那么多的项目>我。顺便说一句:
a)下次标记抛出异常的行(据我所知,可能是第一个
get()
b)使用小写字母作为变量名称:
connections
代替连接数
Where do you set the
i
value? Anyway, the onlyget()
you do usei
as index, so it is clear thatConnections
does not have as much items asi
.BTW:
a) Next time mark at which line the exception is thrown (from what I see, probaly the first
get()
b) use lowercase for variable names:
connections
instead ofConnections
问题是
i
未在本地声明,因此您将永远递增某些实例变量。在本地声明它并将其设置为零。在修复此代码取得任何实际进展之前,我建议您将其清理干净:
connections
而不是Connections
connections
7 > 8
是真的 - 当然它永远是真的!for (City city : from.getConnections())
city
,而不是theCity
The problem is that
i
is non declared locally, so you're forever incrementing some instance variable. Declare it locally and set it to zero.Before you can make any real progress fixing this code, I advise you clean it up:
connections
notConnections
connections
7 > 8
is true - of course it's always true!for (City city : from.getConnections())
city
, nottheCity