Java 具有优先级队列/ArrayList的IndexOutOfBoundsException
我正在做这个项目来做Dijkstra的算法(这看起来像是很多代码,但我必须使用项目的其他给定类和限制),我使用一个优先级队列,将当前顶点的邻居放入队列,该队列按顶点之间的最短距离排列优先级。它在大多数情况下都可以正常工作,但在将邻居坐标添加到优先级队列(pq)时,一旦它命中7个元素,就会在其中一个neighbor.add()行中抛出ArrayOutOfBoundsException。邻域数组的长度永远不会超过4,每个循环都会重新创建,因此我认为这不是ArrayList删除问题。优先级队列是否有问题,或者它实际上是数组列表?我对这两种方法都比较陌生,所以这是我第一次深入研究它们 我已经尝试过尽可能多地更改优先级队列和ArrayList的创建方式以及它们的创建/更新位置,并且在不更改整个代码的情况下仍能正常工作。如果我注释掉pq.add(nb)行,那么它没有这个例外,这使我进一步相信这就是我的问题所在Java 具有优先级队列/ArrayList的IndexOutOfBoundsException,java,arraylist,indexoutofboundsexception,priority-queue,Java,Arraylist,Indexoutofboundsexception,Priority Queue,我正在做这个项目来做Dijkstra的算法(这看起来像是很多代码,但我必须使用项目的其他给定类和限制),我使用一个优先级队列,将当前顶点的邻居放入队列,该队列按顶点之间的最短距离排列优先级。它在大多数情况下都可以正常工作,但在将邻居坐标添加到优先级队列(pq)时,一旦它命中7个元素,就会在其中一个neighbor.add()行中抛出ArrayOutOfBoundsException。邻域数组的长度永远不会超过4,每个循环都会重新创建,因此我认为这不是ArrayList删除问题。优先级队列是否有问
Comparator<Coordinate> compareCoord = new Comparator<Coordinate>(){
public int compare(Coordinate a, Coordinate b){
if(a.getTerrainCost() > b.getTerrainCost()) return 1;
if(a.getTerrainCost() < b.getTerrainCost()) return -1;
else return 0;
}
};
PriorityQueue<Coordinate> pq = new PriorityQueue<>(compareCoord);
------------------------------------------------------------------------------
//Loop used to repeat through all the vertices
while(!unVisited.isEmpty()){
//Set current vertex to next in PQ and add/remove from appropriate lists
Coordinate smallest = pq.poll();
....
List<Coordinate> neighbor = new ArrayList<Coordinate>();
if(r!=0) neighbor.add(map.cells[r-1][c]);
if(r!=rows) neighbor.add(map.cells[r+1][c]); //Line of thrown exception
if(c!=0) neighbor.add(map.cells[r][c-1]);
if(c!=columns) neighbor.add(map.cells[r][c+1]);
//Run for loop for each neighbor of vertex
for(Coordinate nb : neighbor){
//Check to make sure the neighbor has not already been visited
if(!visited.contains(nb)){
//Check path length of the current vertex to the neighbor
int index = coords.indexOf(nb);
Coordinate n = coords.get(index);
int nCost = n.getTerrainCost();
int altPath = totalCosts.get(smallest) + nCost;
//If path is shorter, update variables and add neighbor to priority queue
if(altPath < totalCosts.get(nb)){
totalCosts.put(nb,altPath);
prevCoord.put(nb,smallest);
pq.add(nb); //If commented out, program runs with no exception
}
}
}
-----------------------------------------------------------------------------
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at pathFinder.DijkstraPathFinder.<init>(DijkstraPathFinder.java:73)
at PathFinderTester.main(PathFinderTester.java:294)
Line 73 is commented to find where exception is coming from.
比较器compareCoord=新比较器(){
公共整数比较(坐标a,坐标b){
如果(a.getterracincost()>b.getterracincost())返回1;
if(a.getterracincost()
错误行包含map.cells[r+1][c]
,因此请检查此单元格2d数组的尺寸,以确定r+1
和c
是否是导致此错误的原因错误行包含map.cells[r+1][c]
,因此请检查此单元格2d数组的尺寸,以确定是否是r+1
和c
导致此错误错误行包含map.cells[r+1][c]
,所以检查这个单元格2d数组的尺寸是否是r+1
和c
造成的。我不知道为什么我最初没有考虑更多,但两天后我意识到这是对网格坐标长度的错误计算。谢谢。我已经补充了这一点作为答案,请接受我的回答t、 错误行包含map.cells[r+1][c]
,所以检查这个单元格2d数组的尺寸是否是r+1
和c
造成的。我不知道为什么我最初没有考虑更多,但两天后我意识到这是对网格坐标长度的错误计算。谢谢。我已经补充了这一点作为答案,请接受我的回答T