2017-05-25 87 views
1

我試圖將此循環轉換爲遞歸方法。
這種迭代版本的作品:(遞歸操作來之後)將for循環轉換爲遞歸方法java

 public static int subCompareTwoCol(JobS j1, int index1, int index2, int indexTab1){ //Almost same method as above but with modifications when it encounters a 0. 
     int indexData = 0;            
     int cost = j1.jobS[index1].length;          
     int nZeros = numberCounter(j1.jobS[index2], 0); 

     //index used under to not do the operations too many times. 
     int tab1Index = 0; 


     while(indexData<j1.jobS[index1].length){ 

      if(j1.jobS[index1][indexData] != 0){ 
        assert(index2<6); 
        int j = numberCounter(j1.jobS[index1], j1.jobS[index1][indexData]); 
        int k = numberCounter(j1.jobS[index2], j1.jobS[index1][indexData]); 

         if(j <= k){ 
          cost-=j; 
         } 
         if(j > k){ 
          cost-=k; 
         } 

        indexData += j; 

      }else{ 

       //Initialize 3 tabs, stocks them (previous column, actual column and next column). 
       int[] tab1 = j1.jobS[indexTab1]; 
       int[] tab2 = j1.jobS[index1]; 
       int[] tab3 = j1.jobS[index2]; 

       //I want to convert this part ! 
       //For every numbers from the first tab (starts at the index so it doesnt count the same tool twice if multiple 0's). 
       for(int i=tab1Index; i<Gui.toolN; i++){ 
        tab1Index++; 
        if(isIn(tab3, tab1[i]) == true && isIn(tab2, tab1[i]) == false){ 
         //Modifications to cost (and -=1 to nzeros because we "change" a 0 to the new tool). 
         nZeros-=1; 
         cost -=2; 
         tools.add(tab1[i]); 

         break; 

        } 
       } 
//This is what i think the code would look for replacing the for loop. 
//     int i=0; 
//     boolean b = false; 
//     assert(tab2[indexData]==0); 
//     recurs(j1, index1, index2, indexTab1, tab1, tab2, tab3, nZeros, cost, i, tab1Index, b); 
//     i=0; 
       indexData++; 
      } 
     } 

我嘗試在遞歸:

public static void recurs(JobS j1, int index1, int index2, int indexTab1, int[] tab1, int[] tab2, int[] tab3, int nZeros, int cost, int i, int tab1Index, boolean b){ //j1 is the input JobS, start b is false, start i with 0. 

     i=Integer.valueOf(tab1Index); 
     if(i<Gui.toolN){ 
      tab1Index++; 


      if(isIn(tab3, tab1[i]) == true && isIn(tab2, tab1[i]) == false){ 
       //Modifications to cost (and -=1 to nzeros because we "change" a 0 to the new tool). 
       nZeros-=1; 
       cost -=2; 
       tools.add(tab1[i]); 

       return; 
      }else{ 
       i++; 
       recurs(j1, index1, index2, indexTab1, tab1, tab2, tab3, nZeros, cost, i, tab1Index, b); 
      } 
      return; 

     } 

我不知道發生了什麼事錯了。我試圖從迭代版本複製所有功能,但我無法實現它的功能。 我在做什麼錯?

+0

編輯:剛剛發現,方法結束時,我在方法中改變的值會回到原來的狀態,這可能是問題,任何人都知道如何繞過這個問題? – dfghdf89

+0

你的基本情況是什麼?換句話說,遞歸會停止的條件是什麼? –

+0

當我 dfghdf89

回答

1

基本問題

他們不會「回到自己的老態」 - 這些變量被銷燬(釋放回內存堆),當您退出方法實例。

那些是本地變量 - 當你輸入方法時分配,退出時釋放;一些是輸入參數,並從調用的參數中接收它們的初始值。因此,他們不會「回到他們的舊狀態」。你看到的是前一個方法實例中未改變的狀態。

每次調用例程時,都會向堆棧中添加另一個實例。每個實例都有自己的變量版本,這些變量恰好共享名稱。他們做而不是股價值。

SOLUTION

您通過保持模塊化編程的基本原則,解決此問題:您傳遞值與參數列表的方法;您可以使用return列表返回值。例如,您的最終else條款可能是這個樣子:

child_result = recurs(j1, index1, index2, indexTab1, 
         tab1, tab2, tab3, nZeros, 
         cost, i, tab1Index, b); 
tools.add(child_result); 
return tools; 

讀你的代碼的功能;你可能很需要tools.add(child_result)以外的東西來積累適當的值。不過,我相信我已經給了你目前給你帶來麻煩的基本原則。

+0

謝謝你的男人我希望我在解決這個問題之前有過這個,但是現在我沒有力量去改變數百個行再次^^ 無論如何,我會記住這下次謝謝 – dfghdf89

+0

這就是爲什麼所有創造者發明了全局變化命令和編輯器宏。 :-) – Prune

+0

那些是什麼?他們似乎很有幫助 – dfghdf89

0

問題來自指針,我用作參數的變量只是在方法中保持不變,並在結束時回到原來的狀態。我通過將變量設置爲靜態類變量並在需要時重置它們來繞過此問題,但來自Prune的解決方案看起來更好,更不麻煩。