Java 遞歸函數(shù)多次返回,返回值被覆蓋

如下代碼是有問題的,直到某次遞歸檢查FileName沒有問題那么就不生成新名字再檢查了,直接返回,這是我們想要的,但是之前失敗的調(diào)用也會逐一出棧且反回一個值,應(yīng)為返回了我們每次調(diào)用的局部變量,那么就有問題了,返回多次,想要的值被覆蓋。

改正:每次返回一個全局變量,代碼中打開注釋就好了。

    final String txtNameFirstPart = "New Text Document";
    final String txtNameSecondPart = ".txt";
 //   private String theTrueNewName = "";
    private String getTheTXTNewName( String newFileName, int checkNameTimes ){
       //  theTrueNewName = newFileName;
        if(arrayadapter!=null  && arrayadapter.getM_Items()!=null && arrayadapter.getM_Items().size()>0){
            List<ObjItem> items = arrayadapter.getM_Items();
            for(ObjItem item : items){
                if(item.ObjName.equals(newFileName)){
                    checkNameTimes++;
                    getTheTXTNewName(txtNameFirstPart+" ("+checkNameTimes+")"+txtNameSecondPart, checkNameTimes);
                    break;
                }
            }
        }
        //return theTrueNewName;
        return newFileName;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容