Home > Mobile >  How output matrix with a single loop (java)
How output matrix with a single loop (java)

Time:01-06

how to output the values ​​of a two-dimensional array in the form of a matrix using single loop (it is forbidden to use the methods of the Arrays class or third-party libraries).

int[][] data = {{1,2,3},{4,5,6,7,8},{10}};

output to the console as a matrix!

1 2 3

4 5 6 7 8

10

CodePudding user response:

This is how you can approach this task.

First write down the loops that you would normally use to do this:

for (int i = 0; i < data.length; i  ) {
    for (int j = 0; j < data[i].length; j  ) {
        System.out.print(data[i][j]   " ");
    }
    
    System.out.println();
}

Now I say that it is possible to rewrite this code so that there is only a single loop. In order to do this, I will spell down what for loops actually do with variables:

int i = 0;

while (i < data.length) {
    int j = 0;
    
    while (j < data[i].length) {
        System.out.print(data[i][j]   " ");
        j  ;
    }
    
    System.out.println();
    i  ;
}

Then I will move the j variable out of the outer loop:

int i = 0;
int j = 0;

while (i < data.length) {
    while (j < data[i].length) {
        System.out.print(data[i][j]   " ");
        j  ;
    }
    
    System.out.println();
    i  ;
    j = 0;
}

And finally I decide that it doesn't actually matter in which loop the code runs as long as it runs under the same conditions on the variables and the loop... loops.

int i = 0;
int j = 0;

while (i < data.length) {
    if (j < data[i].length) {
        System.out.print(data[i][j]   " ");
        j  ;
    } else {
        System.out.println();
        i  ;
        j = 0;
    }
}

The last step is not entirely straightforward, but you can't deny the similarity with the previous piece.

Any nested loop can be unrolled with such a technique. You move all the variables outside and replace whiles with ifs here and there and voila!

CodePudding user response:

int[][] data = {{1, 2, 3}, {4, 5, 6, 7, 8}, {10}};

        int currSubArrayNum = 0;

        for (int i = 0; currSubArrayNum < data.length; i  ) {
            System.out.print(data[currSubArrayNum][i] " ");
            if (i == data[currSubArrayNum].length - 1) {
                currSubArrayNum  ;
                i = -1;
                System.out.println();
            }

        }

CodePudding user response:

The trick is to start with element [x][y], and for each step, print out (x,y) and increment y. If y is out-of-bounds (i.e. equal to or greater than [x].length), then increment x and reset y to 0 (and print out a newline).

void matrix(int[][] array) {
    int dim1 = 0;
    int dim2 = 0;
    while (dim1 < array.length) {
        if (array[dim1].length > 0) {
            System.out.print(array[dim1][dim2]);
            System.out.print(" ");
        }
        dim2  ; // Move to next element of sub-array
        
        if (dim2 >= array[dim1].length) {
            dim1  ;
            dim2 = 0;
            System.out.println();
        }
    }
}

CodePudding user response:

The most common loops solution is 2 loops (one nested within the other) like so:

for (int i=0; i<data.length(); i  ) {
    for (int j=0; j<data[i].length(); i  ) {
        System.out.printf("%d ", data[i][j]);
    System.out.println();

Edit: If in your desire for 1 loop you mean no inner loop either, the only way that comes up to me is with a loop that runs for each print needed (equal to the length of all the inner arrays combined). Together with a few if statements it goes like so:

int[][] data = {{1,2,3},{4,5,6,7,8},{10}};
int len = data[0].length   data[1].length   data[2].length;
int j = 0;
int h = 0;
for (int i=0; i<len; i  ) {
    if (h > 0 && h           
  •  Tags:  
  • Related