2016-03-07 155 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