[英]How to convert ArrayList<String[]> to multidimensional array String[][]?
I have a collection of String[] values, for example:我有一个 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);
And I need convert this to multidimensional array String[][].我需要将其转换为多维数组 String[][]。 When I try this:
当我尝试这个时:
String[][] arr = (String[][])values.toArray();
I get a ClassCastException
.我得到一个
ClassCastException
。
How can I solve this problem?我怎么解决这个问题?
What about this (this does not require Java 11 while toArray(String[][]::new)
requires)这个怎么样(这不需要Java 11 而
toArray(String[][]::new)
需要)
values.toArray(new String[0][0]);
That method is:那个方法是:
/**
* 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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.