'Getting the class of the components of an array
If I have
public <T> doSomething(T[] array)
{
}
how can I get T.class from array?
If I do array.getClass() that gets me T[].class instead.
Solution 1:[1]
If you want to do this for multi-dimensional arrays the following recursive code will work
public static Class<?> getArrayType(Object array) {
Object element = Array.get(array, 0);
if (element.getClass().isArray()) {
return getArrayType(element);
} else {
return array.getClass().getComponentType();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
