2

我有一个二维数组列表,如下所示

   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"}
}
4

5 回答 5

4
final String[][] r = new String[childrenSuperList.size()][];
int i = 0;
for (ArrayList<String> l : childrenSuperList) 
  r[i++] = l.toArray(new String[l.size()]);
于 2012-05-26T20:09:38.930 回答
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));
于 2012-05-26T20:18:58.480 回答
0

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

ArrayList listArray = new ArrayList();

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

String []strArray = new String[3];
listArray.toArray(strArray);
于 2012-05-26T20:11:05.507 回答
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++;
            }
        }
    }

}
于 2012-05-26T20:14:31.267 回答
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));
     }

}

于 2016-05-19T05:26:31.093 回答