Say I have a method that accepts an instance of a java Object (Java.lang.Object) as a parameter. Is there a way of determining if this passed in Object is an array (of anything) and then iterating over it?
This is what I'm currently trying to do
public static void isThisAnArray(Object x) {
if (x instanceof Object[]){
for (Object item : x) {
//do something with item
}
}
}
But I get "Can only iterate over an array or an instance of java.lang.Iterable" on the for (Object item : x)
line Which makes sense, since I'm passing in an Object and not an array of Objects. But what if that object is an array of objects? Sorry if this is confusing but is there a way to figure out if a passed in java object is an array?