Better way to write generic method to convert List> to T[][] I was working through getting myself past the creation of a generic method, that converts a List&lt;T&gt; or List&lt;List&lt;T&gt;&gt;, to T[] or T[][] respectively. Well, let's just consider a maximum dimension of 2 for now.

List&lt;T&gt; to T[] is quite straight-forward:

public static &lt;T&gt; T[] listToArray(List&lt;T&gt; list, Class&lt;T&gt; clazz) {

    /**** Null Checks removed for brevity ****/

    T[] arr = (T[]) Array.newInstance(clazz, list.size());

    for (int i = 0; i &lt; list.size(); ++i) {
        arr[i] = list.get(i);
    }
    return arr;
}


Now I can call this method as:

List&lt;String&gt; list = new ArrayList&lt;&gt;(Arrays.asList("rohit", "jain"));
String[] arr = listToArray(list, String.class);




List&lt;List&lt;T&gt;&gt; to T[][] is what gives problems:

Now, here's the problem. Consider the code:

public static &lt;T&gt; T[][] multiListToArray(List&lt;List&lt;T&gt;&gt; listOfList, Class&lt;T&gt; clazz) {

    // Here I don't know what size to give for 2&lt;sup&gt;nd&lt;/sup&gt; dimension
 // T[][] arr = (T[][]) Array.newInstance(clazz, listOfList.size(), ?);

    // So, I tried this. But this of course is not type safe
 // T[][] arr = (T[][]) new Object[listOfList.size()][];

    /* Only alternative I had was to iterate over the listOfList to get the
       maximum out of all the column sizes, which I can give as 2&lt;sup&gt;nd&lt;/sup&gt;
       dimension later on. 
    */
    int maxCol = 0;

    for (List&lt;T&gt; row: listOfList) {
        if (row.size() &gt; maxCol) {
            maxCol = row.size();
        }
    }

    // Now I can pass `maxCol` as 2&lt;sup&gt;nd&lt;/sup&gt; dimension
    T[][] arr = (T[][]) Array.newInstance(clazz, listOfList.size(), maxCol);

    for (int i = 0; i &lt; listOfList.size(); ++i) {
        List&lt;T&gt; row = listOfList.get(i);
        arr[i] = listOfList.get(i).toArray((T[])Array.newInstance(clazz, row.size()));
    }

    return arr;
}


Now, this is what I want to avoid - double iteration of List&lt;List&lt;T&gt;&gt; to be able to create the array. Is there any possibility?
Since you are creating new instance of arrays in your second loop, you don't need to know the size of the "inner list". Your "outer array" has to be as big as the "outer list". In a second phase, you instanciate the "inner arrays".

There's a simple way do this:

public &lt;T&gt; T[][] multiListToArray(final List&lt;List&lt;T&gt;&gt; listOfList, final Class&lt;T&gt; classz) {
    final T[][] array = (T[][]) Array.newInstance(classz, listOfList.size(), 0);

    for (int i = 0; i &lt; listOfList.size(); i++) {
        array[i] = listOfList.get(i).toArray((T[]) Array.newInstance(classz, listOfList.get(i).size()));
    }

    return array;
}


More over, as you can see in the code above, you can provide a 0-length array to toArray(T[]) method.

This piece of code:

final List&lt;List&lt;String&gt;&gt; test = new ArrayList&lt;List&lt;String&gt;&gt;();
test.add(new ArrayList&lt;String&gt;(Arrays.asList("a", "b", "c")));
test.add(new ArrayList&lt;String&gt;(Arrays.asList("d", "e", "f", "g")));
test.add(new ArrayList&lt;String&gt;(Arrays.asList("h", "i")));

for (final String[] innerArray : multiListToArray(test, String.class)) {
    System.out.println(Arrays.toString(innerArray));
}


Will produce this output:


  [a, b, c]
  [d, e, f, g]
  [h, i]


EDIT: There is an Ideone example.

EDIT 2: Code and Ideone updated to comply with OP needs.