1

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.

1
  • @mdoran3844 - Incorrect. for(Something s : someList) iterates over each item in a List or Array. s.test and s.test2 represent the properties of each successive item in the list. Commented Aug 23, 2013 at 0:50

2 Answers 2

9

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

Object[][] array = data.toArray(new Object[data.size()][]);
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers to the pleasant surprises waiting to be discovered when one reads the documentation... I was a little surprised that this would work, but I just tested it and it works great. +1
1
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];
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.