使用递归而不是循环打印嵌套数组中的元素

huangapple go评论72阅读模式
英文:

Printing elements in nested array with recursion instead of loop

问题

以下是已经翻译好的部分:

我一直在尝试使用递归而不是循环来打印以下数组中的元素

int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}}
public class RecursionDoubleLoop {
    void loop(int[][]a,int x,int y)
    {
        int n=a.length;
        if(x<n)
        {

            if(y<a[x].length)
            {

                System.out.println(a[x][y]+" ");

                y+=1;
                if(y<a[x].length)
                loop(a, x, y);
            }
            y=0;
            x+=1;
            /*if(x>=n)
            {
                System.exit(0);
            }*/
            if(x<n) {
            loop(a, x, y);}
        }
    }
    public static void main(String[] args) {
        RecursionDoubleLoop obj= new RecursionDoubleLoop();
        int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}};
        obj.loop(a, 0, 0);
    }

}
现在期望的输出是

2 -36 98 21 55 2 5 4 7 6 101
我的输出是

2 -36 98 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101
尝试调试但最终不得不取消注释 `System.exit(0)` 函数

如果有人能指出错误将非常有帮助

希望这有助于您理解问题所在。

英文:

I have been trying to print elements in

int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}}

with the help of recursion instead of a loop. Now i have a piece of code with me but it prints extra unwanted elements.

public class RecursionDoubleLoop {
	void loop(int[][]a,int x,int y)
	{
		int n=a.length;
		if(x&lt;n)
		{
			
			if(y&lt;a[x].length)
			{
				
				System.out.println(a[x][y]+&quot; &quot;);
				
				y+=1;
				if(y&lt;a[x].length)
				loop(a, x, y);
			}
			y=0;
			x+=1;
			/*if(x&gt;=n)
			{
				System.exit(0);
			}*/
			if(x&lt;n) {
			loop(a, x, y);}
		}
	}
	public static void main(String[] args) {
		RecursionDoubleLoop obj= new RecursionDoubleLoop();
		int[][]a= {{2,-36,98},{21,55},{2,5,4,7,6},{101}};
		obj.loop(a, 0, 0);
	}

}

Now the Expected Output is

2 -36 98 21 55 2 5 4 7 6 101

My output

2 -36 98 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101 21 55 2 5 4 7 6 101 101 101 101 101 2 5 4 7 6 101 101 101 101 101

Tried debugging but ultimately had to uncomment the System.exit(0) function.

It will be very helpful if someone can point out the error.

答案1

得分: 2

你离答案如此之近,以至于告诉你解决方案会让人感到疼痛。你只需要在检查 y < a[x].length 处简单地进行 return 即可。这是因为对 loop 的递归调用将会增加 y 直到条件为 false,最终会继续增加 x。非常接近了。

if (y < a[x].length) {
    loop(a, x, y);
    return;
}

输出

2
-36
98
21
55
2
5
4
7
6
101
英文:

You're so close that it hurts to tell you the solution. All you need to do is simply return in the check for y &lt; a[x].length. This is because the recursive call to loop is going to increase y until this is false and ultimately proceed to increase x. So close man.

if (y &lt; a[x].length) {
    loop(a, x, y);
    return;
}

Output

2 
-36 
98 
21 
55 
2 
5 
4 
7 
6 
101 

答案2

得分: 2

你需要在进入下一行时,将内部的代码放在一个 else 块中。另外,有一些检查是不需要的,因为它们会被递归捕获。

void loop(int[][] a, int x, int y) {
    int n = a.length;
    if (x < n) {
        if (y < a[x].length) {
            System.out.print(a[x][y] + " ");
            loop(a, x, y + 1);
        } else {
            loop(a, x + 1, 0);
        }
    }
}

我是怎么发现的呢?我在方法的第一行打印了 "loop " + x + " " + y,以找出你的代码有什么问题。很容易看出你的实现是正确的,但在增加 x 时没有停止。

如果你想要一个相似的实现(基本上是条件反转),你可以尝试这个:

void loop(int[][] a, int x, int y) {
    if (x >= a.length) return;
    if (y >= a[x].length) {
        loop(a, x + 1, 0);
    } else {
        System.out.print(a[x][y] + " ");
        loop(a, x, y + 1);
    }
}
英文:

You need to wrap your inner code in an else block when going to the next row. Also, a few checks are not needed there, since they will be caught by recursion.

void loop(int[][] a, int x, int y) {
    int n = a.length;
    if (x &lt; n) {
        if (y &lt; a[x].length) {
            System.out.print(a[x][y] + &quot; &quot;);
            loop(a, x, y + 1);
        } else {
            loop(a, x + 1, 0);
        }
    }
}

How I figured out? I printed &quot;loop &quot; + x + &quot; &quot; + y on the first line of the method to figure out what is wrong with your code. It was easy to see that your implementation was good but it was not stopping when increasing x.

If you want a compart implementation(basically, inverted if conditions), you can try this:

void loop(int[][] a, int x, int y) {
    if (x &gt;= a.length) return;
    if (y &gt;= a[x].length) {
        loop(a, x + 1, 0);
    } else {
        System.out.print(a[x][y] + &quot; &quot;);
        loop(a, x, y + 1);
    }
}

答案3

得分: 0

检查x是否达到行限制或y是否大于列限制,然后从递归返回。

检查y是否达到列限制,然后开始新的递归以获取其他数组,其中x+1y0,然后从递归返回。

因此,第二个条件会增加x,而内部递归会增加y

void loop(int[][] a, int x, int y) {
    if (x >= a.length || y >= a[x].length)
        return;
    if (y == a[x].length) {
        System.out.println();
        loop(a, x + 1, 0);
        return;
    }

    System.out.print(a[x][y] + " ");
    loop(a, x, y + 1);
}

输出

2 -36 98
21 55
2 5 4 7 6
101
英文:

Check if the x reaches the rows limit or y is greater than columns limit
then return from the recursion.

, Check if the y reaches the columns limit then start new recursion to get the other arrays with x+1 and y 0 then return from the recursion.

, So the second condition will increase x
and the inner recursion will increase y

    void loop(int[][] a, int x, int y) {
        if (x &gt;= a.length || y &gt; a[x].length)
            return;
        if (y == a[x].length) {
            System.out.println();
            loop(a, x + 1, 0);
            return;
        }

        System.out.print(a[x][y] + &quot; &quot;);
        loop(a, x, y + 1);
    }

, output

2 -36 98
21 55
2 5 4 7 6
101

huangapple
  • 本文由 发表于 2020年5月19日 21:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61892231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定