Method duplicates output

笑着哭i 提交于 2019-12-02 11:42:23

First, ifAtRoot() will return true only if page is null because you cannot compare objects (including strings) using ==. You should use .equals() instead:

public static boolean ifAtRoot(Page page, Page root) {
    return (page == null || root.getPath().equals(page.getPath()));
}

In your case first call of ifAtRoot() returned false, so you called it second time recursively passing brend that just has been created. The second call creates brend again and appends bc (that contains previously created brend) to it. The second call of ifAtRoot() for your luck returns true. Otherwise you'd enter infinite recursion and finish with StackOverflowError.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!