简体   繁体   中英

Printing elements in nested array with recursion instead of loop

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<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);
    }

}

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.

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 < 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 < a[x].length) {
    loop(a, x, y);
    return;
}

Output

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

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 < n) {
        if (y < a[x].length) {
            System.out.print(a[x][y] + " ");
            loop(a, x, y + 1);
        } else {
            loop(a, x + 1, 0);
        }
    }
}

How I figured out? I printed "loop " + x + " " + 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 >= 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);
    }
}

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 >= 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);
    }

, output

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM