Java 嵌套列表到数组的转换

发布于 2024-07-09 16:52:20 字数 663 浏览 7 评论 0原文

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

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

血之狂魔 2024-07-16 16:52:20
//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);
}

我对 JTable 一无所知,但是只需几行即可将列表列表转换为数组。

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

千柳 2024-07-16 16:52:20

特别是对于 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);
    }
}

注意:这是最基本的实现; 为了简洁起见,省略了错误检查。

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

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

烟花肆意 2024-07-16 16:52:20

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

Collection.toArray (IntFunctionGenerator) 方法是 Java 11 中的新方法。

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

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

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

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

七分※倦醒 2024-07-16 16:52:20

要将嵌套列表转换为二维对象数组,可以使用以下代码:

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文