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

我有一个 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);

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

String[][] arr = (String[][])values.toArray();

我得到一个ClassCastException.

我怎么解决这个问题?

回答

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

values.toArray(new String[0][0]);

那个方法是:

/**
 * 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.


以上是如何将ArrayList&lt;String[]&gt;转换为多维数组String[][]?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>