I'm creating a TableModel 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,
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);
}
i.e. creating an array of ArrayList
s, each ArrayList
representing one column, or:
ArrayList<Object[]> rows = new ArrayList<Object[]>();
// Each ArrayList element is one row.
public Object getValueAt(int row, int column) {
return rows.get(row)[column];
}
i.e. creating one ArrayList that holds arrays, each of which represent one row.
Any ideas which of these is more efficient in terms of speed or storage? Alternative 1 requires extending N ArrayList
s with each added row, while alternative 2 requires extending just one ArrayList
but also creating a new array of length N (to represent the new row). Or is there an obvious, better solution?