title: finding maximum elements of columns in java 2D array of variable row length
body: <p>I have a 2-dimensional array which has rows of different lengths. I want to write a method that returns a new array which consists of the maximum elements of columns. If this was a simple n x m array, it would be easy but since the rows are variable length, I can't come up with a solution to account for the different number of elements in columns. </p>
  
  <p>For example, the array looks like this:</p>
  
  <pre><code>int[][] test = { { 0, 1, 4, 5, 6, 8 }, 
                   { 4, 5, 8, 3, 9 },
                   { 3, 6, 2 } 
                 };
  </code></pre>
  
  <p>The expected result would then be:</p>
  
  <pre><code>int[] result =  {4, 6, 8, 5, 9, 8};
  </code></pre>
  
  <p>I have got the code to find the maximum elements of rows but I'm out of ideas how to adjust it for the columns.</p>
  
  <pre><code>int[] result = new int[m.length];
  
        for (int x = 0; x &lt; m.length; x++) {
           result[x] = 0;
           for (int y = 0; y &lt; m[x].length; y++) {
              if (result[x] &lt; m[x][y]) {
                 result[x] = m[x][y];
              } 
           } 
        } 
  </code></pre>
  
  <p>Any help would be appreciated</p>
  
  <p>EDIT: I realized now that the first thing to do is finding the row with maximum number of elements since that defines the size of the new array. From there.. should probably take the elements of a row and compare them with the elements at the same position in the new array. And do this with every row. Then it doesn't matter how short the other rows are. Am I on the right way?</p>