Java 嵌套列表到数组的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/371839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:07:23  来源:igfitidea点击:

Java nested list to array conversion

javaarrayslistjtable

提问by MrG

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)?

将数据从嵌套列表转换为对象数组(可用作 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!

非常感谢!

回答by Patrick

//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.

我对 JTable 一无所知,但是可以通过几行将列表列表转换为数组。

回答by Michael Myers

For JTablein particular, I'd suggest subclassing AbstractTableModellike so:

对于JTable特别,我建议子类AbstractTableModel,如下所示:

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[][].

使用这样的模型,您不必担心无意义的转换为Object[][].

回答by Ole V.V.

Java 11 answer.

Java 11 答案。

    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]]

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

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

Collection.toArray?(IntFunction<T[]> generator)方法是 Java 11 中的新方法。

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

当然,您也可以在 Java 8+ 中使用流。只需使用此映射:

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

(The List.ofmethod was introduced in Java 9.)

(该List.of方法是在 Java 9 中引入的。)