[英]IndexOutOfBoundsException when running Dijkstra's algorithm implementation
不確定導致此IndexOutOfBoundsException
發生的原因。 如果我對所有addFlightPath()
參數進行硬編碼,代碼就可以完美地找到,但是當我嘗試使用for循環來填充flightPaths
arrayList時,會拋出IndexOutOfBoundsException
。 我可能錯過了一些小東西,但我不確定它可能是什么。
在addFlightPath
方法中調用flightPaths.add(path)
拋出異常
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);
}
// Uncommenting this section of code allows the program to function normally
// addFlightPath("Path_AB", 0, 1, 800);
// addFlightPath("Path_BC", 1, 2, 900);
// addFlightPath("Path_CD", 2, 3, 400);
// addFlightPath("Path_BF", 1, 5, 400);
// addFlightPath("Path_DE", 3, 4, 300);
// addFlightPath("Path_EB", 4, 1, 600);
// addFlightPath("Path_CE", 2, 4, 200);
// addFlightPath("Path_DC", 3, 2, 700);
// addFlightPath("Path_EB", 4, 1, 500);
// addFlightPath("Path_FD", 5, 3, 200);
// addFlightPath("Path_DE", 3, 4, 400);
// addFlightPath("Path_CE", 2, 4, 300);
}
private void addFlightPath(String pathId, int sourceAirport, int destAirport, int distance)
{
Edge path = new Edge(pathId, nodes.get(sourceAirport), nodes.get(destAirport), distance);
flightPaths.add(path); //IndexOutOfBounds exception is thrown here
}
}
堆棧跟蹤
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at core.dijkstras2.DijkstrasController.addFlightPath(DijkstrasController.java:83)
at core.dijkstras2.DijkstrasController.populateDijkstrasGraph(DijkstrasController.java:63)
at core.dijkstras2.DijkstrasController.<init>(DijkstrasController.java:19)
at core.dijkstras2.DDriver.main(DDriver.java:10)
因此,這個IndexOutOfBounds的原因是由於有問題的for循環阻止了nodes
ArrayList被正確填充。
要解決這個問題,我只需更改此代碼:
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);
}
並且將第二個塊移動到單獨的for循環中,這允許第一個for循環首先填充arraylist,然后添加飛行路徑。
for (int i = 0; i < (fdb.getDatabaseSize()); i++)
{
Vertex location = new Vertex("Node_" + i, nodeNumberToNodeLetter(i));
nodes.add(location);
}
for (int i = 0; i < fdb.getDatabaseSize(); i++)
{
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);
}
正如評論中所提到的,您的例外似乎源於以下行:
Edge path = new Edge(pathId, nodes.get(sourceAirport), nodes.get(destAirport), distance);
讓我們來看看你的populateDijkstrasGraph()
函數(違規部分):
for (int i = 0; i < (fdb.getDatabaseSize()); i++)
{
Vertex location = new Vertex("Node_" + i, nodeNumberToNodeLetter(i));
nodes.add(location);
在這里添加nodeNumberToNodeLetter(i)
給出的位置。
int sourceAirport = nodeLetterToNodeNumber(journey.getOriginAirport());
int destinationAirport = nodeLetterToNodeNumber(journey.getDestinationAirport());
在這里,您將獲得前兩個機場節點的整數值。 但是,我們只添加了一個節點! (上面的nodes.add(location)
)。
因此,如果sourceAirport == 0
和destinationAirport == 1
, nodes
當前只包含一個Vertex
,則nodes.get(sourceAirport)
將起作用,而nodes.get(destinationAirport)
將按預期拋出IndexOutOfBoundsException
。
解決此問題的一種方法是在嘗試填充節點之間的邊之前填充節點列表。
編輯:如果您還沒有,則應在IDE中啟用行號。 它使調試更容易。 此外,您應該熟悉調試和斷點 - 這樣可以讓您很快找到上述錯誤。
一些猜測工作,但我認為你的代碼的工作版本看起來像這樣:
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);
}
// Uncommenting this section of code allows the program to function normally
addFlightPath("Path_AB", 0, 1, 800);
addFlightPath("Path_BC", 1, 2, 900);
addFlightPath("Path_CD", 2, 3, 400);
// etc.
這樣可以正常工作,因為您有一個完全填充的nodes
List。 如果將addFlightPath
移動到for
-loop中,則nodes
在循環的第一次迭代中只包含一個元素。 因此,對nodes.get(1)
的調用將因IndexOutOfBounds
異常而失敗。
您可能需要循環兩次:
for (int i = 0; i < (fdb.getDatabaseSize()); i++)
{
Vertex location = new Vertex("Node_" + i, nodeNumberToNodeLetter(i));
nodes.add(location);
}
for (int i = 0; i < (fdb.getDatabaseSize()); i++)
{
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);
}
當您執行循環時,始終從您使用的列表的0到大小-1開始。 如果你有一個5位的列表,循環從0到4.也許它是你的問題?
試着把:
for (int i = 0; i < (fdb.getDatabaseSize()-1); i++)
{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.