Bob Bob - 22 days ago 7
Java Question

How can I check if an array of unknown type is empty?

Say I have an Object which I know is an array. How can I find out if it contains any elements?

public void myMethod (Object o) {
if (o.getClass().isArray()) {
// how can I check if the array contains any elements?
}
}

Answer
java.lang.reflect.Array.getLength(o)

Javadoc

Comments