Is there any alternative way to identify instance of multidimensional object without hardcode on it?
//source
import java.util.LinkedList;
import java.util.List;
public class Test {
public static <T> boolean isPrimitiveWrapper(T p_obj) throws Exception {
if (Number.class.isInstance(p_obj) || Number[].class.isInstance(p_obj)) {
return true;
} else if (Boolean.class.isInstance(p_obj) || Boolean[].class.isInstance(p_obj)) {
return true;
} else if (Character.class.isInstance(p_obj) || Character[].class.isInstance(p_obj)) {
return true;
}
return false;
}
public static void main(String[] args) throws Exception {
Integer[][][] a = {{{0}}, {{1}}, {{0}}};
println(isPrimitiveWrapper(a));
println(isPrimitiveWrapper(a[0]));
println(isPrimitiveWrapper(a[0][0]));
println(isPrimitiveWrapper(a[0][0][0]));
}
public static <T> void println(T p_t) {
System.out.println(p_t);
}
}
//Actual Result
false
false
true
true
//Expected Result
true
true
true
true
Based on the example above, we have no problem to deal with non-array & single dimension array object, but it is fail to identify multidimensional array object.
It is too ugly to hardcode the number of dimension.