Java – Convert list to array. java.lang.ArrayStoreException

javalistmultidimensional-array

There is a list:

List<Integer[]> myList = new ArrayList<Integer[]>();

It contains a sigle entry, but might contain multiple entries:

myList = [[2,null,1,null,null,3,6,1,1]]

I need to convert this list into the array Integer[][], but the conversion fails due to nulls:

Integer[] myArr = myList.toArray(new Integer[myList.size()]);

How to solve this issue?

Edit#1

I need to get:

myArr = [2,null,1,null,null,3,6,1,1]

Best Solution

Try this (assuming you have actually the List<Integer[]> you talked about in your comment):

List<Integer[]> myList = new ArrayList<Integer[]>();
myList.add(new Integer[] {2,null,1,null,null,3,6,1,1} );

Integer[][] myArr = myList.toArray(new Integer[myList.size()][]);

If you convert a list of arrays to an array, you'll get a 2 dimensional array and thus your parameter should be one too.