jack jack - 5 months ago 25
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?


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) {
        else {
            print(a blank);