2015-04-01 16 views
0

我想使用遞歸方法找出最接近我的二維數組中的空元素,如果輸入索引已經有一個元素。例如,我打電話給我的方法tryPlant(int X, int Y)把一個符號在我的2D(32×32)陣列:StackOverflowError在遞歸調用期間尋找最接近的二維數組中的空索引

  1. ecoArray [X] [Y] == 「」。 (空)然後用「〜」填充
  2. 如果除了「。」之外還有一個元素,則遞歸查找同一行中的下一個空白點,然後是列。

該方法的代碼是:

public void tryPlant(int X, int Y){ 
    if (this.ecoArray[X][Y] == ".") { 
     this.ecoArray[X][Y] = "~"; 
    }else{ 
     if((Y - 1) >= 0) { 
      tryPlant(X, Y - 1); 
     } 
     else if((Y + 1) <= 32){ 
      tryPlant(X, Y + 1); 
     }else if((X - 1) >= 0) { 
      tryPlant(X - 1, Y); 
     }else if((X + 1) <= 32){ 
      tryPlant(X + 1, Y); 
     } 
    } 
} 

我打電話的方法,這樣其他類:

Plant p1; 

private void initPlants(){ 
     int size = p1.initPop; 
     for (int i = 0; i < size; i++) { 
      int randX = randGen(); 
      int randY = randGen(); 
      Plant plant = new Plant(randX, randY, this.ecoArray); 
     } 
    } 

randGen()方法返回0-31之間的隨機整數。

有時,隨機生成器會給我索引,使它們不會與其他對象發生衝突。 (該|障礙物)這方面的一個例子:

Planting trees

我想知道爲什麼我得到一個計算器錯誤,我能做些什麼來解決它。如果您需要我的代碼的其他部分,請詢問。我對Java很新。

編輯

public static int left = 0; 
public static int right = 0; 

    public void tryPlant(int X, int Y){ 
     if (this.ecoArray[X][Y] == ".") { 
      this.ecoArray[X][Y] = "~"; 
     }else{ 

      if((Y - 1) >= 0) { 
       this.left++; 
       tryPlant(X, Y - 1); 
      } 
      else if((Y + this.left + 1) <= 32){ 
       tryPlant(X, Y + this.left + 1); 
       this.left = 0; 
      }else if((X - 1) >= 0) { 
       this.right++; 
       tryPlant(X - 1, Y); 
      }else if((X + this.right + 1) <= 32){ 
       tryPlant(X + + this.right + 1, Y); 
       this.right = 0; 
      } 
     } 
    } 
+2

您的功能錯誤。想象一下,例如你以X = 0,Y = 1開始,並且(0,0)和(0,1)單元都被佔用。首先你移到(0,0)。接下來的迭代讓你回到(0,1),這種情況會無限期地重複。 每次遞歸調用都會在堆棧上創建一個條目,並最終溢出,這就是爲什麼會出現錯誤。 你需要處理你的算法,它根本不工作。 – Dima 2015-04-01 02:06:34

+0

哦拍,你是對的。我在設定該方法時正在考慮邊界條件。我會繼續思考並回復你。 – 2015-04-01 02:19:03

+0

我試圖用兩個靜態整數修正你提到的狀態。這可能不是最好的方法,所以請告訴我是否問題比這個更大。 – 2015-04-01 02:37:05

回答

-2

大約發生的事情是:每次函數被調用時它被存儲在存儲器的堆棧。當函數返回時,所存儲的內容被清除。但是,如果調用的函數太多,則會填充內存併發送一個stackoverflow錯誤。

你得到一個stackoverflow錯誤,因爲你的算法中有一個循環的地方。這導致太多致電tryPlant()

  • 首先,當你找到一種你已經通過的方式時,返回。 (其中char == '~'
  • 二,哪裏有障礙,返回。 (其中char == '|'
  • 第三,你不嘗試四面八方:

    if ((Y - 1) >= 0) { 
         this.left++; 
         tryPlant(X, Y - 1); 
        } else if ((Y + this.left + 1) <= 32){ 
         tryPlant(X, Y + this.left + 1); 
         this.left = 0; 
        } else if ((X - 1) >= 0) { 
         this.right++; 
         tryPlant(X - 1, Y); 
        } else if ((X + this.right + 1) <= 32){ 
         tryPlant(X + + this.right + 1, Y); 
         this.right = 0; 
        } 
    

我想這和它似乎工作:

public class Test { 

    private char[][] ecoArray; 

    public Test() { 
     String[] stringArray = new String[] { 
     "||.|.||..|..||.|.||||||||..||.||", 
     "||||....||..|||..||....|.||..|||", 
     "||||||...|.|||...||.|..||..||||.", 
     "||...||.|.|||.||||.|||||.|...||.", 
     "|.|....|.|||||||||..||.|.|.||...", 
     "..|||.||||...|..||.||..|..||||.|", 
     "..|.||||||..||.||||..|||.|.|...|", 
     "|||||.||.|||...||...||..||.|||..", 
     "||||.|..||||||..|.|||...||.||.|.", 
     "|||.|||||.|||||.||||.|....||||||", 
     "||...||||||.|.|||||||||||.|.|.||", 
     "|.|.||||||||.||||....|.||||.||||", 
     "||..||.||||.|..||.|||..||.|.||||", 
     "..||..|..||.|.|||..|||..|||||.|.", 
     "||||.|.||.||||.|||||..|||.|.....", 
     "..|.|.|||..|||..||.||||.|||.|..|", 
     "||||.|..|||||||.|||||.||.|.|....", 
     "..|...||...|||||.|...|..|...|||.", 
     "..|||||||..||...||||||..|..|||||", 
     "||||..||.|.|||||.||||.|||||.||..", 
     "|||||.||||.|....||||....||.||...", 
     "||..||.|||||.||||||..||..|....||", 
     "|.||||.||..|...|.|..|||.|.|||.||", 
     "...||||.|..|||.|||..|.||...|.|||", 
     ".||||.|..|.|..||..||..||..||||||", 
     "|||.|||..|||..||||.||||.|.||.|||", 
     "|||||.||...|.|.|.||...|||..|.|||", 
     ".||||.|.|.|||...|||.|||.....||||", 
     "|||.|||.|.|...||.|....||||.|.|||", 
     ".||||||.|||||||...||..|||.||||.|", 
     "||||.|||||.|.||.||||..|.|||.||||", 
     "|.|.||||....|.||||||||.|||||.|.|" 
     }; 

     this.ecoArray = new char[32][32]; 
     int index = 0; 
     for (String s : stringArray) { 
      this.ecoArray[index] = s.toCharArray(); 
      ++index; 
     } 
    } 

    public void tryPlant(int X, int Y){ 
     if (this.ecoArray[X][Y] == '~' || this.ecoArray[X][Y] == '|') { 
      return; 
     } 
     if (this.ecoArray[X][Y] == '.') { 
      this.ecoArray[X][Y] = '~'; 

      if ((Y - 1) >= 0) { 
       tryPlant(X, Y - 1); 
      } 
      if((Y + 1) < 32){ 
       tryPlant(X, Y + 1); 
      } 
      if((X - 1) >= 0) { 
       tryPlant(X - 1, Y); 
      } 
      if((X + 1) < 32){ 
       tryPlant(X + 1, Y); 
      } 
     } 
    } 

    private void displayInConsole() { 
     for (char[] cl : this.ecoArray) { 
      System.out.println(cl); 
     }  
    } 

    public static void main(String[] args) { 
     for (int x = 0; x < 32; x++) { 
      for (int y = 0; y < 32; y++) { 
       Test t = new Test(); 
       t.tryPlant(x, y); 
       System.out.println("After tryPlant('" + x + "','" + y + "'): "); 
       t.displayInConsole(); 
      } 
     } 
    } 

} 

它在幹什麼,你需要什麼?我真的不知道你要開始和想要停止的地方...

結果太大了對不起...

+0

迪瑪的迴應對我來說更有意義。我的功能幾乎來回循環,因爲我目前設定的條件是我在考慮邊界情況時所做的。 – 2015-04-01 02:25:21

+0

迪瑪的迴應和我一樣......但是也許我不完全清楚......解釋?在任何情況下,這不是一個壞的反應,所以不要downvote它... – 2015-04-01 02:26:20

+0

我敢肯定,你不會嘗試我的解決方案之前downvoting它。 – 2015-04-01 02:29:00