Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To print a variable length 2-D array, your inner loop runs from <code>0</code> to <code>the current row</code> length : -</p> <pre><code>for (int i = 0; i &lt; arr.length; i++) { for (int j = 0; j &lt; arr[i].length; j++) { // Closely see inner loop System.out.println(arr[i][j]); } } </code></pre> <p><code>arr[i]</code> is the current row. And <code>arr[i].length</code> gives the number of columns in that row.</p> <p><strong>You can infer it like this: -</strong></p> <ul> <li><code>arr</code> is a 2-D array.</li> <li>So, each element of your <code>arr</code> is a <code>1-D array</code>.</li> <li>So, <code>arr[i]</code> is a 1-D array. Which represents each row.</li> <li>To get number of columns in each row, you do <code>arr[i].length</code></li> </ul> <p>Now, you can apply the same thing in your problem.</p> <hr> <p>Actually, your <code>for loop</code> is running wrongly. Your <code>outer loop</code> is running from <code>col = 0 to col &lt; uneven.length</code>, but it should run from: - <code>row = 0 to row &lt; uneven.length</code></p> <p>So, your for loop should be like: -</p> <pre><code>for ( int row=0; row &lt; uneven.length; row++ ) { System.out.print("Row " + row + ": "); for( int col=0; col &lt; uneven[row].length; col++ ) { System.out.print( uneven[row][col] + " "); } System.out.println(); } </code></pre> <hr> <h2>UPDATED : -</h2> <p>Ok, I got your question wrong first. If you want to print <code>column wise</code>, you can use this code: -</p> <pre><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 &lt; uneven.length; row++ ) { if (uneven[row].length &gt; max) { max = uneven[row].length; } } System.out.println(max); for (int i = 0; i &lt; max; i++) { for (int j = 0; j &lt; uneven.length; j++) { if (uneven[j].length &lt;= i) { continue; } System.out.print(uneven[j][i] + " "); } System.out.println(); } </code></pre> <p>First you need to find the max among all number of columns in each row. </p> <p>Then run the loop again, from 0 to max columns. Now, since you have a lop for <code>columns</code>, now you need another one for <code>rows</code>. And that will be your inner loop.</p> <p>Now, in inner loop, you cannot just print the array element at the <code>(j, i)</code> index, because the current row might not have <code>max</code> number of columns. So, you need to put an <code>if-condition</code> to check that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload