2016-03-07 154 views
-3
public LinkedList<MazeCoord> getPath() { 

     return getPath(); 
    } 

public class MazeCoord { 
    final private int row; // final (non-static) means it can't be updated once   
    final private int col; //  it's initialized 

    // create a MazeCoord with the given row and column 
    public MazeCoord(int row, int col) { 
     this.row = row; 
     this.col = col; 
    } 

    // get the row of this MazeCoord 
    public int getRow() { return row; } 

    // get the col of this MazeCoord; 
    public int getCol() { return col; } 

} 

爲什麼當我嘗試在我的Eclipse中運行這個控制檯時,控制檯會提示我堆棧溢出?JAVA鏈接列表

任何人都可以告訴我原因嗎?

+0

本網站有搜索功能。在發佈新問題之前使用它。 – Raedwald

回答

6

getPath()正在調用自己,這會導致無限的調用鏈,當堆棧溢出時會結束。正確的遞歸方法必須具有停止條件。

+0

謝謝,但我如何重用鏈表,如果它不能返回自己。 即如果我想使用getPath()。add(...),我怎樣才能得到整個鏈表? – WALES