Chris Valenzuela
Chris Valenzuela

Reputation: 39

Printing Column

I'm having a problem printing columns. When the code reaches "100" it stops reading what is below it because it's empty:

public class Column{

  public static void main( String[] arg )
  {

    int[][] uneven = 
        { { 1, 3, 100, 6, 7, 8, 9, 10},
          { 0, 2},
          { 0, 2, 4, 5},
          { 0, 2, 4, 6, 7},
          { 0, 1, 4, 5 },
          { 0, 1, 2, 3, 4, 5, 6 }};

    for ( int col=0; col < uneven.length; col++ )
    {
      System.out.print("Col " + col + ": ");
      for( int row=0; row < uneven.length; row++ ) 
        System.out.print( uneven[row][col] + " ");
      System.out.println();
    }
  }
}

What should I do so that it will continue reading the column?

Upvotes: 2

Views: 1539

Answers (2)

Rohit Jain
Rohit Jain

Reputation: 213371

To print a variable length 2-D array, your inner loop runs from 0 to the current row length : -

for (int i = 0; i < arr.length; i++) {

    for (int j = 0; j < arr[i].length; j++) {  // Closely see inner loop

        System.out.println(arr[i][j]);
    }
}

arr[i] is the current row. And arr[i].length gives the number of columns in that row.

You can infer it like this: -

  • arr is a 2-D array.
  • So, each element of your arr is a 1-D array.
  • So, arr[i] is a 1-D array. Which represents each row.
  • To get number of columns in each row, you do arr[i].length

Now, you can apply the same thing in your problem.


Actually, your for loop is running wrongly. Your outer loop is running from col = 0 to col < uneven.length, but it should run from: - row = 0 to row < uneven.length

So, your for loop should be like: -

for ( int row=0; row < uneven.length; row++ )
{
   System.out.print("Row " + row + ": ");

   for( int col=0; col < uneven[row].length; col++ ) { 
     System.out.print( uneven[row][col] + " ");
   }
   System.out.println();
}

UPDATED : -

Ok, I got your question wrong first. If you want to print column wise, you can use this code: -

int[][] uneven = 
    { { 1, 3, 100, 6, 7, 8, 9, 10},
      { 0, 2},
      { 0, 2, 4, 5},
      { 0, 2, 4, 6, 7},
      { 0, 1, 4, 5 },
      { 0, 1, 2, 3, 4, 5, 6 }};

    int max = -1;
    for ( int row = 0; row < uneven.length; row++ ) {
        if (uneven[row].length > max) {
            max = uneven[row].length;
        }
    }
    System.out.println(max);
    for (int i = 0; i < max; i++) {
        for (int j = 0; j < uneven.length; j++) {
            if (uneven[j].length <= i) {
                continue;
            }
            System.out.print(uneven[j][i] + " ");
        }
        System.out.println();
    }

First you need to find the max among all number of columns in each row.

Then run the loop again, from 0 to max columns. Now, since you have a lop for columns, now you need another one for rows. And that will be your inner loop.

Now, in inner loop, you cannot just print the array element at the (j, i) index, because the current row might not have max number of columns. So, you need to put an if-condition to check that.

Upvotes: 3

hcg
hcg

Reputation: 652

Replace:

for( int row=0; row < uneven.length; row++ ) 

with:

for( int row=0; row < uneven[col].length; row++ ) 

Upvotes: 0

Related Questions