将 ArrayList 转换为包含不同长度数组的二维数组 [英] Convert ArrayList into 2D array containing varying lengths of arrays
本文介绍了将 ArrayList 转换为包含不同长度数组的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有:
ArrayList<ArrayList<String>>
其中包含 x 个数组列表,其中包含另一个 y 个字符串.. 演示:
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
其中 index 是指包含字符串的数组索引.
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}}
非常感谢.
推荐答案
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()]);
}
其中 arrayList
是您的 ArrayList
(或任何 List
,更改>
for
循环中的第一行相应地)
where arrayList
is your ArrayList<ArrayList<String>>
(or any List<List<String>>
, change the first line inside the for
loop accordingly)
这篇关于将 ArrayList 转换为包含不同长度数组的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文