jack jack - 10 days ago 6
Java Question

how can i print vertically a 2d array with different row length in java

I know how to print vertically a 2d array when all the rows have the same length but what happens when an array is like this:{{2,3,4},
{6,23,56,78},
{12,11,43,54,77}};
how i am supposed to loop through the vertical values?

Answer

Each column will tell you the length of the array. Use that array length as the to test to see if the column has a value for that. If so, print it, otherwise print a blank.

int[][] matrix;   //column, then row
for (int row=0; row<maxRows; row++) {
    for (int col=0; col<maxCol; i++) {
        int[] column = matrix[col];
        if (row < column .length) {
            print(column[row]);
        }
        else {
            print(a blank);
        }
    }
}