Java运行递归程序时出现错误越界异常

发布于 2024-12-07 02:34:42 字数 1782 浏览 3 评论 0原文

我正在学习递归作为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-12-14 02:34:42

在哪里设置 i 值?不管怎样,唯一使用 i 作为索引的 get() ,所以很明显 Connections 没有像 i 那么多的项目>我。

顺便说一句:

a)下次标记抛出异常的行(据我所知,可能是第一个 get()

b)使用小写字母作为变量名称:connections 代替连接数

Where do you set the i value? Anyway, the only get() you do use i as index, so it is clear that Connections does not have as much items as i.

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 of Connections

陈独秀 2024-12-14 02:34:42

问题是 i 未在本地声明,因此您将永远递增某些实例变量。在本地声明它并将其设置为零。

在修复此代码取得任何实际进展之前,我建议您将其清理干净:

  • 使用前导小写字母命名变量 - 即 connections 而不是 Connections
  • 在有意义的地方使用局部变量- 如 connections
  • 删除无意义的代码,例如测试 if 7 > 8 是真的 - 当然它永远是真的!
  • 使用正确的块缩进
  • 使用“foreach”循环迭代集合: 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:

  • Name variables with a leading lowercase - ie connections not Connections
  • Use local variables where it makes sense - like connections
  • Remove nonsense code, such as the test if 7 > 8 is true - of course it's always true!
  • Use proper indentation of blocks
  • Iterate over collections using a "foreach" loop: 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文