11

What is the most efficient way to convert data from nested lists to an object array (which can be used i.e. as data for JTable)?

List<List> table = new ArrayList<List>();

for (DATAROW rowData : entries) {
    List<String> row = new ArrayList<String>();

    for (String col : rowData.getDataColumn())
        row.add(col);

    table.add(row);
}

// I'm doing the conversion manually now, but
// I hope that there are better ways to achieve the same
Object[][] finalData = new String[table.size()][max];
for (int i = 0; i < table.size(); i++) {
    List<String> row = table.get(i);

    for (int j = 0; j < row.size(); j++)
        finalData[i][j] = row.get(j);
}

Many thanks!

4 Answers 4

13
//defined somewhere
List<List<String>> lists = ....

String[][] array = new String[lists.size()][];
String[] blankArray = new String[0];
for(int i=0; i < lists.size(); i++) {
    array[i] = lists.get(i).toArray(blankArray);
}

I don't know anything about JTable, but converting a list of lists to array can be done with a few lines.

Sign up to request clarification or add additional context in comments.

Comments

8

For JTable in particular, I'd suggest subclassing AbstractTableModel like so:

class MyTableModel extends AbstractTableModel {
    private List<List<String>> data;
    public MyTableModel(List<List<String>> data) {
        this.data = data;
    }
    @Override
    public int getRowCount() {
        return data.size();
    }
    @Override
    public int getColumnCount() {
        return data.get(0).size();
    }
    @Override
    public Object getValueAt(int row, int column) {
        return data.get(row).get(column);
    }
    // optional
    @Override
    public void setValueAt(Object aValue, int row, int column) {
        data.get(row).set(column, aValue);
    }
}

Note: this is the most basic implementation possible; error-checking is omitted for brevity.

Using a model like this, you don't have to worry about pointless conversions to Object[][].

2 Comments

Good answer. However, double-check that the data passed to this model isn't visible elsewhere, and prone to ninja-editing. It probably isn't, but if it is, it may need to be cloned here.
Good point. I thought of that, but DefaultTableModel actually doesn't check either (it uses Vectors). Anyway, this is the most basic implementation possible. ;)
4

Java 11 answer.

List<List<String>> table = List.of(List.of("A", "B"), List.of("3", "4"));
String[][] finalData = table.stream()
        .map(arr -> arr.toArray(String[]::new))
        .toArray(String[][]::new);
    
System.out.println(Arrays.deepToString(finalData));

[[A, B], [3, 4]]

The Collection.toArray​(IntFunction<T[]> generator) method is new in Java 11.

Of course you may also use a stream in Java 8+. Just use this mapping instead:

.map(arr -> arr.toArray(new String[0]))

(The List.of method was introduced in Java 9.)

Comments

0

To convert a nested list to a two-dimensional object-array, you can use this code:

public Object[][] ToObjectMatrix(List<List<String>> data) throws IllegalArgumentException {
    if(data == null) {
        throw new IllegalArgumentException("Passed data was null.");
    }

    Object[][] result = new Object[data.size()][];
     
    for(int i = 0; i < data.size(); i++) {
        result[i] = data.get(i).toArray();
    }
     
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.