Java 在递归调用中查找2D数组中最近的空索引时使用StackOverflowerError
如果输入索引已经有一个元素,我尝试使用递归方法来计算2D数组中最近的空元素。例如,我调用我的方法Java 在递归调用中查找2D数组中最近的空索引时使用StackOverflowerError,java,Java,如果输入索引已经有一个元素,我尝试使用递归方法来计算2D数组中最近的空元素。例如,我调用我的方法tryPlant(intx,inty)在我的2D(32x32)数组中放置一个符号: ecoArray[X][Y]==”(空),然后用“~”填充它 如果除“.”之外还有一个元素,则递归地首先查找同一行中的下一个空位,然后查找列 该方法的代码为: public void tryPlant(int X, int Y){ if (this.ecoArray[X][Y] == ".") {
tryPlant(intx,inty)
在我的2D(32x32)数组中放置一个符号:
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);
}
}
}
大致情况是:每次调用函数时,它都存储在内存堆栈中。当函数返回时,存储的内容将被清除。但是,如果调用太多函数,则会填满内存并发送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; }
它在做你需要的吗?我真的不明白你想从哪里开始,在哪里停止如果((Y-1)>=0){ 这个.左++; 胰蛋白酶(X,Y-1); }如果((Y+this.left+1)=0){ 这个; 胰蛋白酶(X-1,Y); }否则如果((X+this.right+1)=0){ 胰蛋白酶(X,Y-1); } 如果((Y+1)<32){ 胰蛋白酶(X,Y+1); } 如果((X-1)>=0){ 胰蛋白酶(X-1,Y); } 如果((X+1)<32){ 胰蛋白酶(X+1,Y); } } } 私有void displayincole(){ for(char[]cl:this.ecoArray){ 系统输出打印项次(cl); } } 公共静态void main(字符串[]args){ 对于(int x=0;x<32;x++){ 对于(int y=0;y<32;y++){ 测试t=新测试(); t、 胰蛋白酶(x,y); System.out.println(“在tryPlant(“+x+”,“+y+”):”之后; t、 displayincole(); } } } }
结果太大,抱歉…您的函数错误。例如,假设从X=0、Y=1开始,并且(0,0)和(0,1)单元格都被占用。首先移动到(0,0)。下一次迭代会将您带回(0,1),这种情况会无限期地重复。每次递归调用都会在堆栈上创建一个条目,最终它会溢出,这就是为什么会出现错误。你需要改进你的算法,它根本不起作用。噢,天哪,你说得对。当我建立这个方法时,我在考虑边界条件。我会继续考虑,然后再给你回复。我试图用两个静态整数来修复你提到的状态。这可能不是最好的方法,所以请告诉我问题是否比这更严重。好吧,我不确定这是唯一的问题(可能不是),但是,至少,你的方法除了“左”和“右”之外,似乎还需要“上”和“下”,不是吗?另外,考虑到我前面提到的同一个案例,我看不出您的解决方案有什么帮助:从(0,1)开始,移动到(0,0),跳回到(0,2)(假设它也被占用),移动到(0,1)。。。重新开始。你为什么要递归地做这件事?这可能不是解决此问题的最直观的方法。上下操作是通过更改X(列)值来处理的。我会想一个更好的方法来移动我的植物。迪玛的反应对我来说更有意义。我的函数基本上是来回循环的,因为我目前设定的条件是在考虑边界情况的情况下设定的。。。但也许我不太清楚。。。解释无论如何,这不是一个坏的反应,所以不要投反对票…我确信你在投反对票之前不会尝试我的解决方案。对不起,但当人们不加评论地投反对票时,我很难过,因为这完全没有用。只是仇恨者。回到主题:如果你尝试我的解决方案会发生什么。好奇心不好,我知道。:)是我投了反对票。答案不清楚,最后的建议是错误的。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; } } }
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(); } } } }