Java generics, convert nested list to array I have to interface to some old code using nested arrays, so I have to convert my nested Lists to nested arrays. Any idea, suggestions, improvements are warmly welcomed.

public static &lt;E&gt; int getNestedListMaxSize(List&lt;List&lt;E&gt;&gt; list){
    int result = 0;
    if (list != null &amp;&amp; list.size() &gt; 0){
        for (List&lt;E&gt; innerList : list){
            result = Math.max(result, innerList == null ? 0 : innerList.size());
        }
    }
    return result;
}

@SuppressWarnings("rawtypes")
public static &lt;E&gt; Class getNestedListClass(List&lt;List&lt;E&gt;&gt; list){
    Class result = null;
    if (list != null &amp;&amp; list.size() &gt; 0){
        for (List&lt;E&gt; innerList : list){
            if (innerList != null &amp;&amp; innerList.size() &gt; 0){
                result = innerList.get(0).getClass();
                break;
            }
        }
    }
    return result;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static &lt;E&gt; E[][] convertNestedList(List&lt;List&lt;E&gt;&gt; list){
    E[][] result = null;
    Class clazz = getNestedListClass(list);
    if (clazz != null){
        result = (E[][]) Array.newInstance(clazz, list.size(), getNestedListMaxSize(list));

        for(int i=0; i &lt; list.size(); i++) {
            E[] element = null;
            if (list.get(i) != null){
                E[] dummy = (E[]) Array.newInstance(clazz, list.get(i).size());
                element = list.get(i).toArray(dummy);
            }
            result[i] = element;
        }           
    }
    return result;
}

public static void main(String[] args) {
    List&lt;List&lt;String&gt;&gt; list = new ArrayList&lt;List&lt;String&gt;&gt;();
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size()-1).add("0.0");
    list.get(list.size()-1).add("0.1");
    list.get(list.size()-1).add("0.2");
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size()-1).add("1.0");
    list.get(list.size()-1).add("1.1");
    list.get(list.size()-1).add("1.2");
    list.get(list.size()-1).add("1.3");
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size()-1).add("2.0");
    list.get(list.size()-1).add("2.1");

    String [][] array = convertNestedList(list);

    System.out.println(Arrays.deepToString(array));
}
I'd use guard clauses to check whether the list parameter is null or not. Furthermore, the list.size() &gt; 0 check looks unnecessary in the first two methods, the result will be same if you omit it.

Anyway, are you sure that null is an acceptable parameter here? Wouldn't it better to throw an exception in this case?

public static &lt;E&gt; int getNestedListMaxSize(final List&lt;List&lt;E&gt;&gt; list) {
    if (list == null) {
        return 0;
    }
    int result = 0;
    for (final List&lt;E&gt; innerList: list) {
        if (innerList == null) {
            continue;
        }
        result = Math.max(result, innerList.size());
    }
    return result;
}


In the getNestedListClass I'd use guard clauses too and a return instead of the break.

@SuppressWarnings("rawtypes")
public static &lt;E&gt; Class getNestedListClass(final List&lt;List&lt;E&gt;&gt; list) {
    if (list == null) {
        return null;
    }

    for (final List&lt;E&gt; innerList: list) {
        if (innerList == null || innerList.isEmpty()) {
            continue;
        }
        return innerList.get(0).getClass();
    }
    return null;
}


I think the return makes the code much easier to read and follow.

In the convertNestedList method 


the dummy variable should have a meaningful name, for example innerListArray,
the default value of result[i] is null, so you don't have to set it to null explicitly if the inner list is null,
list.get(i) is called three times. It could have a local variable.




public static &lt;E&gt; E[][] convertNestedList(final List&lt;List&lt;E&gt;&gt; list) {
    final Class&lt;?&gt; clazz = getNestedListClass(list);
    if (clazz == null) {
        return null;
    }
    final E[][] result = (E[][]) Array.newInstance(clazz, list.size(), 
        getNestedListMaxSize(list));

    for (int i = 0; i &lt; list.size(); i++) {
        final List&lt;E&gt; innerList = list.get(i);
        if (innerList == null) {
            continue;
        }
        final E[] innerListArray = (E[]) Array.newInstance(clazz, innerList.size());
        result[i] = innerList.toArray(innerListArray);
    }
    return result;
}


The main method should be a unit test method with proper assert calls:

@Test
public void testConvertNestedList() {
    final List&lt;List&lt;String&gt;&gt; list = new ArrayList&lt;List&lt;String&gt;&gt;();
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size() - 1).add("0.0");
    list.get(list.size() - 1).add("0.1");
    list.get(list.size() - 1).add("0.2");
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size() - 1).add("1.0");
    list.get(list.size() - 1).add("1.1");
    list.get(list.size() - 1).add("1.2");
    list.get(list.size() - 1).add("1.3");
    list.add(new ArrayList&lt;String&gt;());
    list.get(list.size() - 1).add("2.0");
    list.get(list.size() - 1).add("2.1");

    final String[][] array = ListConvert.convertNestedList(list);

    // @formatter:off
    final String[][] expected = new String[][] { 
            { "0.0", "0.1", "0.2" }, 
            { "1.0", "1.1", "1.2", "1.3" },
            { "2.0", "2.1" } };
    // @formatter:on
    assertArrayEquals(expected, array);
}


It helps detecting regressions.