13

I have array from Field:

Field[] fields = instance.getClass().getDeclaredFields();

for (Field field : fields){    

if((field.getType() == Integer.class[])||(field.getType() == Object.class[]))
{
//...    
} 

}

How do to learn Field is array? (how to know which field is an array (Object[] array)?), Help me, please.

0

2 Answers 2

20

Use Class#isArray():

if (field.getType().isArray()) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

12

You can call isArray() on the Class<T> object of the field's type. To get the type of the element, call getComponentType method.

if((field.getType().isArray()) {
    Class componentType = field.getType().getComponentType();
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.