我是否必须遍历每个元素才能将ArrayList<String[]>
转换为String[][]
,还是有更有效的方式?
答案 0 :(得分:8)
只需将内容作为数组
List<String[]> list = new ArrayList<>();
...
String[][] array = list.toArray(new String[0][0]); // the size here is not important, the array is just an indication of the type to use
答案 1 :(得分:3)
您可以使用.toArray(T[])
。
public static void main(String[] args) {
List<String[]> l = new ArrayList<String[]>();
String[] a = {"lala", "lolo"};
String[] b = {"lili", "lulu"};
l.add(a);
l.add(b);
String[][] r = new String[l.size()][];
r = l.toArray(r);
for(String[] s : r){
System.out.println(Arrays.toString(s));
}
}
输出:
[lala, lolo]
[lili, lulu]