about java: get `String[].class` from `String.class`, what if `String.class` is a “runtime type”? [duplicate]

HRgiger’s answer improved:

@SuppressWarnings("unchecked")
static <T> Class<? extends T[]> getArrayClass(Class<T> clazz) {
    return (Class<? extends T[]>) Array.newInstance(clazz, 0).getClass();
}

Both of them instantiate an array object when invoked. To get the array type, use

Class<?> childType = ...;
Class<?> arrayType = getArrayClass(childType);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)