如何将2D String ArrayList转换为简单的2D字符串数组?

时间:2012-05-26 20:04:32

标签: java arraylist multidimensional-array

我有一个2D arraylist,如下所示

   ArrayList<ArrayList<String>> childrenSuperList = new ArrayList<ArrayList<String>>();
    ...
    childrenList = new ArrayList<String>();
    ...
    childrenSuperList.add(childrenList);

如何将其转换为简单的字符串数组,如

String s[][]= {{"a","b","c"},
{"d","e","f"},
{"a","b","c"}
}

5 个答案:

答案 0 :(得分:4)

final String[][] r = new String[childrenSuperList.size()][];
int i = 0;
for (ArrayList<String> l : childrenSuperList) 
  r[i++] = l.toArray(new String[l.size()]);

答案 1 :(得分:2)

您的列表可以有不同的大小,因此表格不会有固定的大小,但这没问题。

ArrayList<ArrayList<String>> childrenSuperList = new ArrayList<ArrayList<String>>();
// put some data
ArrayList<String> a = new ArrayList<>();
a.add("a1");
a.add("a2");
ArrayList<String> b = new ArrayList<>();
b.add("b1");
b.add("b2");
b.add("b3");

childrenSuperList.add(a);
childrenSuperList.add(b);

// you need to know sizes of array
String[][] array = new String[childrenSuperList.size()][];
int i = 0, j = 0;
for (ArrayList<String> row : childrenSuperList) {
    array[i] = new String[row.size()];
    j = 0;
    for (String str : row) {
        array[i][j] = str;
        j++;
    }
    i++;
}
System.out.println(Arrays.deepToString(array));

答案 2 :(得分:0)

有一种方法可以从ArrayList中获取字符串数组:

ArrayList listArray = new ArrayList();

listArray.add("One");
listArray.add("Two");
listArray.add("Three");

String []strArray = new String[3];
listArray.toArray(strArray);

答案 3 :(得分:0)

以下代码将帮助您获得完美的完整结果。

public static void main(String[] args) {

    ArrayList<ArrayList<String>> val = new ArrayList<ArrayList<String>>();
    int arraySize = 0;
    int upperSize = 0;

    if (val != null) {
        ArrayList<String> arrayList = val.get(0);
        upperSize = val.size();
        if (arrayList != null) {
            arraySize = arrayList.size();
        }
    }

    int i = 0, j = 0;
    String[][] values = new String[upperSize][arraySize];
    if (val != null) {
        for (ArrayList<String> curr : val) {
            if (curr != null) {
                for (String currValue : curr) {
                    values[i][j++] = currValue;
                }
                i++;
            }
        }
    }

}

答案 4 :(得分:0)

import java.util.*;

公共类HelloWorld {

 public static void main(String []args){

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

    ArrayList<String> list=new ArrayList<String>();
    list.add("Joshan Stethem");
    list.add("Bruce Wills");
    list.add("Tom Cruse");
    list.add("Optimus Prime");
    System.out.println("Array List Hollywood : "+list);
    ArrayList<String> list1=new ArrayList<String>();
    list1.add("Salman Khan");
    list1.add("Shahrukh Khan");
    list1.add("AMir Khan");
    list1.add("Fawad Khan");   
     System.out.println("Array List Bollywood : "+list1);
       ArrayList<String> list2=new ArrayList<String>();
    list2.add("Rahat Fateh");
    list2.add("Arijit Singh");
    list2.add("KK");
    list2.add("Ankit Tiwari");   
     System.out.println("Array List Singers : "+list2);
    parentList.add(list);
    parentList.add(list1);
    parentList.add(list2);

 String[][] twodimArray = new String[parentList.size()][];
     int i = 0, j = 0;
 for (ArrayList<String> row : parentList) {
  twodimArray[i] = new String[row.size()];
   j = 0;
   for (String str : row) {
        twodimArray[i][j] = str;
         j++;
        }
       i++;
      }
 System.out.println("List of Artist : "+Arrays.deepToString(twodimArray));
     }

}