title: Getting the array length of a 2D array in Java
body: <p>I need to get the length of a 2D array for both the row and column.  I’ve successfully done this, using the following code:</p>
  
  <pre><code>public class MyClass {
  
   public static void main(String args[])
      {
    int[][] test; 
    test = new int[5][10];
  
    int row = test.length;
    int col = test[0].length;
  
    System.out.println(row);
    System.out.println(col);
      }
  }
  </code></pre>
  
  <p>This prints out 5, 10 as expected.</p>
  
  <p>Now take a look at this line:</p>
  
  <pre><code>  int col = test[0].length;
  </code></pre>
  
  <p>Notice that I actually have to reference a particular row, in order to get the column length.  To me, this seems incredibly ugly.  Additionally, if the array was defined as:</p>
  
  <pre><code>test = new int[0][10];
  </code></pre>
  
  <p>Then the code would fail when trying to get the length.  Is there a different (more intelligent) way to do this?</p>