You can use instanceof .
RelationalExpression: RelationalExpression instanceof ReferenceType
At run time, the result of the instanceof statement is true if the value of RelationalExpression is not null , and the link can be passed to ReferenceType without raising a ClassCastException . Otherwise, the result is false .
This means you can do something like this:
Object o = new int[] { 1,2 }; System.out.println(o instanceof int[]);
You will need to check if there is an instanceof boolean[] object instanceof boolean[] , byte[] , short[] , char[] , int[] , long[] , float[] , double[] or Object[] if you want to define everything types of arrays.
In addition, int[][] is instanceof Object[] , so depending on how you want to handle nested arrays, it can get complicated.
For toString java.util.Arrays has toString(int[]) and other overloads that you can use. It also has deepToString(Object[]) for nested arrays.
public String toString(Object arr) { if (arr instanceof int[]) { return Arrays.toString((int[]) arr); } else
This will be very repetitive (but even java.util.Arrays very repetitive ), but just like in Java with arrays.
see also
- Managing very repetitive code and documentation in Java
- Java Arrays.equals () returns false for two-dimensional arrays.
polygenelubricants Apr 27 '10 at 22:15 2010-04-27 22:15
source share