不是特别确定是什么导致这种IndexOutOfBoundsException情况发生。如果我对所有addFlightPath()参数进行硬编码,代码就可以完美地找到,但是一旦我尝试使用 for 循环来填充flightPathsarrayList,IndexOutOfBoundsException就会抛出一个。我可能遗漏了一些小东西,但我不确定它可能是什么。
flightPaths.add(path)在addFlightPath方法内调用时抛出异常
public class DijkstrasController
{
private FlightDatabase flightDatabase;
private List<Vertex> nodes;
private List<Edge> flightPaths;
public DijkstrasController(FlightDatabase flightDatabase)
{
this.flightDatabase = flightDatabase;
populateDijkstrasGraph(flightDatabase);
}
public String[] runDijkstras(String sourceAirport, String destinationAirport)
{
//Removed for visibility
}
public void populateDijkstrasGraph(FlightDatabase fdb)
{
nodes = new ArrayList<Vertex>();
flightPaths = new ArrayList<Edge>();
for (int i = 0; i < (fdb.getDatabaseSize()); i++)
{
Vertex location = new Vertex("Node_" + i, nodeNumberToNodeLetter(i));
nodes.add(location);
//This block of code throws an IndexOutOfBounds error
AirJourney journey = fdb.getFlightDetails(i);
String pathId = "Path_" + journey.getOriginAirport() + journey.getDestinationAirport();
int sourceAirport = nodeLetterToNodeNumber(journey.getOriginAirport());
int destinationAirport = nodeLetterToNodeNumber(journey.getDestinationAirport());
int distance = journey.getNumberOfMilesToTravel();
addFlightPath(pathId, sourceAirport, destinationAirport, distance);
}
缥缈止盈
慕姐4208626
有只小跳蛙
相关分类