如何将 ArrayList<String[]> 转换为多维数组 String[][]?

stf*_*fxc 5 java collections arraylist classcastexception multidimensional-array

我有一个 String[] 值的集合,例如:

ArrayList<String[]> values = new ArrayList<>();

String[] data1 = new String[]{"asd", "asdds", "ds"};
String[] data2 = new String[]{"dss", "21ss", "pp"};

values.add(data1);
values.add(data2);
Run Code Online (Sandbox Code Playgroud)

我需要将其转换为多维数组 String[][]。当我尝试这个时:

String[][] arr = (String[][])values.toArray();
Run Code Online (Sandbox Code Playgroud)

我得到一个ClassCastException.

我怎么解决这个问题?

fzy*_*cjy 8

这个是什么(这并没有需要Java 11时toArray(String[][]::new)需要)

values.toArray(new String[0][0]);
Run Code Online (Sandbox Code Playgroud)

那个方法是:

/**
 * Returns an array containing all of the elements in this list in proper
 * sequence (from first to last element); the runtime type of the returned
 * array is that of the specified array.  If the list fits in the
 * specified array, it is returned therein.  Otherwise, a new array is
 * allocated with the runtime type of the specified array and the size of
 * this list.
Run Code Online (Sandbox Code Playgroud)