Java チュートリアルの一部として再帰について学んでおり、少し助けを求めています。
直行便がない場合に、ある都市から別の都市に移動する方法を解決する再帰的な Java プログラムを作成する必要があります。
私の最新の問題は、コードの FlightRoute 配列リストに 2 つの都市が含まれていると、範囲外の例外でエラーが発生することです。「IndexOutOfBoundsException Index 2 Size 2」というエラーが発生します
connection 値は、その都市が接続するすべての都市を取得する arrayList であり、flightRoute も、目的地に到達するために移動しなければならなかった都市を追跡する arrayList です。
なぜそれが進まないのか、私には理解できません。
できれば、これについて助けていただければ幸いです。
コードで皆さんをオーバーフローさせたくないので、必要なメソッドを掲載します。さらに必要な場合は、喜んでコードを追加します。
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;
}