@SuppressWarnings("unchecked")
    public static <T> T[] toArray(Iterable<? extends T> list, Class<T> c) {
        int size = -1;
        if(list instanceof Collection<?>){
        	@SuppressWarnings("rawtypes")
			Collection coll = (Collection)list;
        	size = coll.size();
        }
        
        
        if(size < 0){
        	size = 0;
        	// Ugly hack: Count it ourselves
        	for(@SuppressWarnings("unused") T element : list){
        		size++;
        	}
        }
    	
        T[] result = (T[]) Array.newInstance(c, size);
        int i = 0;
        for(T element : list){ // Assumes iteration order is consistent
    		result[i++] = element; // Assign array element at index THEN increment counter
    	}
        return result;
    }