转换的ArrayList到含有阵列的不同长度的二维数组 [英] Convert ArrayList into 2D array containing varying lengths of arrays
本文介绍了转换的ArrayList到含有阵列的不同长度的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有:
的ArrayList< ArrayList的<串GT;>
其中包含了包含字符串的另一个y个。要证明的ArrayList的X号:
0指数:
串1
2串
字串3
指标1:
字串4
指数2:
指数3:
字串5
字串6
在哪里指数是指含有一个字符串数组索引。
我怎样才能将其转化成一个二维数组,它看起来像:
{{字符串1,字符串,STRING3},{串,},{},{STRING5,String6}}
感谢您这么多。
解决方案
的String [] []数组=新的String [arrayList.size()] [];
的for(int i = 0; I< arrayList.size();我++){
ArrayList的<串GT;行= arrayList.get(I)
数组[我] = row.toArray(新的String [row.size()]);
}
其中,的ArrayList
是的ArrayList< ArrayList的<弦乐>>
(或清单<名单<弦乐>>
,修改为
循环内的第一行相应)
So I have:
ArrayList<ArrayList<String>>
Which contains an x number of ArrayLists which contain another y number of Strings.. To demonstrate:
Index 0:
String 1
String 2
String 3
Index 1:
String 4
Index 2:
Index 3:
String 5
String 6
Where index refers to the array index containing a string.
How can I transform this into a 2D array which looks like:
{{String1, String2, String3},{String4}, {}, {String5, String6}}
Thank you so much.
解决方案
String[][] array = new String[arrayList.size()][];
for (int i = 0; i < arrayList.size(); i++) {
ArrayList<String> row = arrayList.get(i);
array[i] = row.toArray(new String[row.size()]);
}
where arrayList
is your ArrayList<ArrayList<String>>
(or any List<List<String>>
, change the first line inside the for
loop accordingly)
这篇关于转换的ArrayList到含有阵列的不同长度的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文