title: ArrayList of arrays vs. array of ArrayLists vs. something similar
body: <p>I'm creating a <a href="http://java.sun.com/javase/6/docs/api/javax/swing/table/TableModel.html" rel="nofollow noreferrer">TableModel</a> which will have a fixed number of columns, but the number of rows will be changing (mostly, increasing as function of time). Which would be better approach to store the data,</p>
  
  <pre><code>ArrayList[] columns = new ArrayList[numberOfColumns];
  // Each array element is one column. Fill each of them with a new ArrayList.
  ...
  public Object getValueAt(int row, int column) {
      return columns[column].get(row);
  }
  </code></pre>
  
  <p>i.e. creating an array of <code>ArrayList</code>s, each <code>ArrayList</code> representing one column, or:</p>
  
  <pre><code>ArrayList&lt;Object[]&gt; rows = new ArrayList&lt;Object[]&gt;();
  // Each ArrayList element is one row.
  
  public Object getValueAt(int row, int column) {
      return rows.get(row)[column];
  }
  </code></pre>
  
  <p>i.e. creating one ArrayList that holds arrays, each of which represent one row.</p>
  
  <p>Any ideas which of these is more efficient in terms of speed or storage? Alternative 1 requires extending N <code>ArrayList</code>s with each added row, while alternative 2 requires extending just one <code>ArrayList</code> but also creating a new array of length N (to represent the new row). Or is there an obvious, better solution?</p>