private int[] findArrayDimensions(List<?> list, Class<?> arrayType) {
        List<int[]> elementDims = new LinkedList<int[]>();
        int dimCount = 0;
        for (Object o : list) {
            if (o != null && o.getClass().isArray() && 
                isAssignableFromArray(arrayType, o.getClass().getComponentType())) {
                final int[] dimensions = findArrayDimensions(o);
                elementDims.add(dimensions);
                dimCount = Math.max(dimCount, dimensions.length);
            }
        }

        int[] dimsArr = new int[dimCount + 1];
        // First dimension length is always size of given list
        dimsArr[0] = list.size();
        // Find the max length of each additional dimension
        for (int[] childDim : elementDims) {
            for (int i = 0; i < childDim.length; i++) {
                dimsArr[i + 1] = Math.max(dimsArr[i + 1], childDim[i]);
            }
        }
        return dimsArr;
    }
private int [ ] findArrayDimensions ( List < ? > list , Class < ? > arrayType ) { List < int [ ] > elementDims = new LinkedList < int [ ] > ( ) ; int dimCount = 0 ; for ( Object o : list ) { if ( o != null && o . getClass ( ) . isArray ( ) && isAssignableFromArray ( arrayType , o . getClass ( ) . getComponentType ( ) ) ) { final int [ ] dimensions = findArrayDimensions ( o ) ; elementDims . add ( dimensions ) ; dimCount = Math . max ( dimCount , dimensions . length ) ; } } int [ ] dimsArr = new int [ dimCount + 1 ] ; dimsArr [ 0 ] = list . size ( ) ; for ( int [ ] childDim : elementDims ) { for ( int i = 0 ; i < childDim . length ; i ++ ) { dimsArr [ i + 1 ] = Math . max ( dimsArr [ i + 1 ] , childDim [ i ] ) ; } } return dimsArr ; }
If the elements of the list are arrays, get the max length of each needed
dimension, otherwise return an int[] containing one element, the length
of the list.