简体   繁体   中英

Java ArrayList<Object[]> to Object[][]

I have data in the format

List<Object[]> data = new ArrayList<Object[]>();

for(Something s : SomeList) {
    Object[] tempList = { s.test, s.test2, s.test3 };

    data.add(tempList);
}

So I have an ArrayList of Objects[] . Now I want to get that ArrayList of objects into an Object[][] so I have the data in the format

Object[][] data = { { test, test2, test3 }, { test4, test5, test6 } };

but I'm not sure how to do that.

The ArrayList step isn't important, it's just the direction I took when trying to figure it out. If you have a better way of getting the data to Object[][] that's great.

您可以使用List#toArray(T[])

Object[][] array = data.toArray(new Object[data.size()][]);
Object[][] data2 = new Object[data.size()][]
for( int i = 0; i < data.size(); ++i)
{
    data2[i] = new Object[data.get(i).length];
    for( int j = 0; j < data.get(i).length; ++j )
    {
        data2[i][j] = data.get(i)[j];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM