jack jack - 1 year ago 69
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},
how i am supposed to loop through the vertical values?

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download