简体   繁体   中英

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

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?

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.

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