convert an arrayList<Object[]> into a nested array

Bertuz :

This is the following idea:

List<Object[]> ret = new ArrayList<>();
ret.add(new Object[]{"a", 1});
Object[][] obj = (Object[][]) ret.toArray();

but it doesn't work: toArray returns Object[]

Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [[Ljava.lang.Object;

Any idea?

khelwood :

There is a version of toArray that accepts an array as an argument. This allows you to specify what type of array you want returned.

Object[][] obj = ret.toArray(new Object[0][]);

You can pass in an array big enough to hold the contents of your list; or you can pass in an array of zero size and the list implementation should return a new array of the same type as the one you passed in.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=76712&siteId=1