【问题标题】:IndexOutOfBoundsException with Priority Queue/ArrayListIndexOutOfBoundsException 与优先队列/ArrayList
【发布时间】:2019-05-18 16:35:21
【问题描述】:

我正在做这个项目来做 Dijkstra 算法(这似乎有很多代码,但我必须使用项目的其他给定类和限制)并且我正在使用优先级队列,它放置当前的邻居顶点进入队列,按顶点间最短距离排序。它在大多数情况下都可以正常工作,但是当将邻居坐标添加到优先级队列(pq)时,一旦它达到 7 个元素,它就会在其中一个邻居.add() 行中引发 ArrayOutOfBoundsException。邻居数组的长度永远不会超过 4,并且每个循环都会重新创建,所以我认为这不是 ArrayList 删除问题。我对优先队列做错了什么还是实际上是数组列表?我对使用这两种方法都比较陌生,所以这是我第一次深入使用它们。

我已尝试尽可能多地更改优先级队列和 ArrayList 的创建方式以及它们的创建/更新位置,并且在不更改整个代码的情况下仍然可以正常运行。如果我注释掉 pq.add(nb) 行,那么它就没有这个异常,这让我进一步相信这就是我的问题所在。


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.

【问题讨论】:

  • 错误的行包含map.cells[r+1][c],因此请检查此单元格二维数组的尺寸是否是r+1c 造成的。
  • 我不知道为什么我最初并没有考虑深入研究,但 2 天后我意识到这是对我的网格坐标长度的错误计算。谢谢。
  • 我已将其添加为答案,请接受。

标签: java arraylist indexoutofboundsexception priority-queue


【解决方案1】:

错误的行包含map.cells[r+1][c],因此请检查此单元格二维数组的尺寸是否是r+1c 造成的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2011-12-20
    • 2010-12-28
    • 2012-03-28
    • 2011-08-21
    相关资源
    最近更新 更多