如何转换ArrayList <arraylist <string>&gt; to String [] []?</arraylist <string>

时间:2014-06-17 14:24:03

标签: java arraylist

您可以使用

ArrayList<String>转换为String[]
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("one");
stringList.add("two");
String[] strings1 = Arrays.copyOf(stringList.toArray(), stringList.toArray().length, String[].class);

但是如何将ArrayList<ArrayList<String>>转换为String[][]

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

1 个答案:

答案 0 :(得分:0)

String[][] strings2 = convertStringArrayList(listOfStringLists);

ArrayList<ArrayList<String>>转换为String[][]方法:

private String[][] convertStringArrayList( ArrayList<ArrayList<String>> listOfStringLists ) {
    String[][] stringGroups = new String[listOfStringLists.size()][];
    for ( int i=0; i< listOfStringLists.size(); i++ ) {
        ArrayList<String> stringArray = listOfStringLists.get(i);
        String[] stringsChildren = Arrays.copyOf(stringArray.toArray(), stringArray.toArray().length, String[].class);
        stringGroups[i] = stringsChildren;
    }
    return stringGroups;
}