user2686811
user2686811

Reputation: 629

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.

Upvotes: 1

Views: 171

Answers (2)

arshajii
arshajii

Reputation: 129507

You can use List#toArray(T[]):

Object[][] array = data.toArray(new Object[data.size()][]);

Upvotes: 9

ningyuwhut
ningyuwhut

Reputation: 629

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

Upvotes: 1

Related Questions