简体   繁体   English

转换ArrayList <ArrayList<String> &gt;到Java中的二维数组

[英]Convert ArrayList<ArrayList<String>> to two dimensional Array in java

How can I convert this type of Arraylist to Two Dimensional Arraylist ? 如何将这种类型的Arraylist转换为二维Arraylist?

ArrayList<ArrayList<String>> two = new ArrayList<ArrayList<String>>();

In android ExpandableListView does not allow to use Arraylist to populate the listview. 在android中ExpandableListView不允许使用Arraylist填充listview。 But I have to dynamically populate the data from JSON web service. 但是我必须从JSON Web服务动态填充数据。 How Can I solve this issue ? 我该如何解决这个问题?

Any help would be great ! 任何帮助都会很棒!

If you're coming from a JSON object, then the data is already in the format that you want. 如果您来自JSON对象,则数据已经具有所需的格式。 It's already an array of arrays. 它已经是一个数组数组。 You can just loop through the first array assigning the second as you go. 您可以循环遍历第一个数组,同时分配第二个数组。

String[][] foo = new String [myJsonObject.getStringArray("arrayOfArrays")).length() ][];
for(int i = 0; myJsonObject.exists("array_" + i); i++){
   foo[i] = myJsonObject.getStringArray("array_" + i);
}

I didn't test this, so the syntax might not be 100% but you get the idea. 我没有对此进行测试,因此语法可能不是100%,但是您明白了。

You just create a Hashmap first and put data to the hashmap. 您只需先创建一个哈希图,然后将数据放入哈希图。 And then Create a Arraylist and put hashmap inside arraylist. 然后创建一个Arraylist并将hashmap放入arraylist中。 That is i done in my case of multidimensional array. 我是在多维数组的情况下完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM