Module for building swing tables I have a module that builds swing tables based on an Object[][], then I have another module that queries an SQLite database and returns a ResultSet.

I have written a method that converts the ResultSet to an ArrayList&lt;Object[]&gt; first, then further into an Object[][]. The reason I do it this way is because you cannot get the row count from the ResultSet without iterating over it.

I'm wondering if there is a more efficient way to build my Object[][] from the ResultSet without having to iterate over the data twice.

Here is the method that performs the conversion:

public Object[][] executeQuery(String query) throws SQLException{
    ResultSet rs = getResultSet(query);
    ResultSetMetaData rsMetaData = rs.getMetaData();
    int columnCount = rsMetaData.getColumnCount();
    ArrayList&lt;Object[]&gt; result = new ArrayList&lt;Object[]&gt;();
    Object[] header = new Object[columnCount];
    for (int i=1; i &lt;= columnCount; ++i){
        Object label = rsMetaData.getColumnLabel(i);
        header[i-1] = label;
    }
    while (rs.next()){
        Object[] str = new Object[columnCount];
        for (int i=1; i &lt;= columnCount; ++i){
            Object obj = rs.getObject(i);
            str[i-1] = obj;
        }
        result.add(str);
    }
    int resultLength = result.size();
    Object[][] finalResult = new Object[resultLength][columnCount];
    finalResult[0] = header;
    for(int i=1;i&lt;resultLength;++i){
        Object[] row = result.get(i);
        finalResult[i] = row;
    }
    return finalResult;
}
Well, you could just call the toArray method of your list to transform the list into an array. Or even better, you could make the table model use a List&lt;Object[]&gt; rather than an Object[][], which would avoid the unnecessary conversion.