简体   繁体   English

在Java中运行递归程序时出现错误超出范围的异常

[英]Error out of bounds exception when running recursive program in Java

I'm learning about recursion as part of a Java tutorial and I am looking for a little help. 我正在作为Java教程的一部分学习递归,并且正在寻找一点帮助。

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. 我们需要制作一个递归Java程序,该程序将计算出没有直飞航班时如何从一个城市到达另一个城市。

My latest issue is Im getting an error out of bounds exception after the code has 2 cities in the flightRoute array List. 我的最新问题是代码在flightRoute数组列表中有2个城市后,Im越界错误。 it gives the error "IndexOutOfBoundsException Index 2 Size 2" 它给出错误“ IndexOutOfBoundsException索引2大小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. 连接值是一个arrayList,它接收与城市连接的所有城市,而flightRoute也是一个arrayList,它跟踪我们为了到达目的地而必须到达的城市。

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

Where do you set the i value? 您在哪里设置i值? Anyway, the only get() you do use i as index, so it is clear that Connections does not have as much items as i . 无论如何,您唯一的get()确实将i用作索引,因此很明显Connections的项没有i

BTW: 顺便说一句:

a) Next time mark at which line the exception is thrown (from what I see, probaly the first get() a)下一个时间标记在哪一行抛出异常(根据我的观察,可能是第一个get()

b) use lowercase for variable names: connections instead of Connections b)对变量名使用小写字母: connections而不是Connections

The problem is that i is non declared locally, so you're forever incrementing some instance variable. 问题是i 没有在本地声明,所以您将永远增加一些实例变量。 Declare it locally and set it to zero. 在本地声明它并将其设置为零。

Before you can make any real progress fixing this code, I advise you clean it up: 在修正此代码之前,请先进行清理:

  • Name variables with a leading lowercase - ie connections not Connections 用前导小写字母命名变量-即connections而不是Connections
  • Use local variables where it makes sense - like connections 在有意义的地方使用局部变量-如connections
  • Remove nonsense code, such as the test if 7 > 8 is true - of course it's always true! 删除废话代码,例如测试7 > 8是否为真-当然总是如此!
  • Use proper indentation of blocks 使用适当的块缩进
  • Iterate over collections using a "foreach" loop: for (City city : from.getConnections()) 使用“ foreach”循环遍历集合: for (City city : from.getConnections())
  • Tend to name your variables the same as the class name, but with a lowercase letter, so city , not theCity 倾向于使用与类名相同的名称来命名变量,但是要使用小写字母,所以city而不是theCity

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM