You get Classto write through getClassand set this class if it is an array type through isArray:
boolean isOneDimensional(Object[] array)
{
for (int i=0; i<array.length; i++)
if (array[i] != null && array[i].getClass().isArray()) return false;
return true;
}
Pay attention to the check nulljust in case.
No, my bad one, the above gives the wrong result:
isOneDimensional(new String[2][])
, , , : , Object[], , , , - Object[], , new String[2][].
, ; , array.getClass().getComponentType().getComponentType():
boolean isOneDimensional(Object[] array) {
if (array.getClass().getComponentType().getComponentType() != null) {
return false;
}
for (int i=0; i<array.length; i++) {
if (array[i] != null && array[i].getClass().isArray()) {
return false;
}
}
return true;
}
, getComponentType null, , Object[], . , , .
String[] o1 = new String[2];
String[][] o2 = new String[2][];
Object[] o3 = new Object[2];
Object[] o4 = new Object[2];
o3[0] = new Object[0];
System.out.println(isOneDimensional(o1));
System.out.println(isOneDimensional(o2));
System.out.println(isOneDimensional(o3));
System.out.println(isOneDimensional(o4));