Java嵌套列表到数组的转换 [英] Java nested list to array conversion

查看:74
本文介绍了Java嵌套列表到数组的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据从嵌套列表转换为对象数组(可以用作JTable的数据)的最有效方法是什么?

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

非常感谢!

推荐答案

//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一无所知,但是只需几行就可以将列表列表转换为数组.

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

这篇关于Java嵌套列表到数组的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆