How to determine if an object is an array without using reflection?

How can I see in Java if an object is an array without using reflection? And how can I iterate over all elements without reflection?

I use Google GWT, so I am not allowed to use reflection :(

I would like to implement the following methods without using refelection:

private boolean isArray(final Object obj) { //??.. } private String toString(final Object arrayObject) { //??.. } 

By the way: I don’t want to use JavaScript so that I can use it in environments without GWT.

+78
java arrays instanceof gwt
Apr 27 '10 at 10:10
source share
5 answers

You can use Class.isArray()

 public static boolean isArray(Object obj) { return obj!=null && obj.getClass().isArray(); } 

This works for both arrays of objects and primitive types.

For toString take a look at Arrays.toString . You will need to check the type of the array and call the corresponding toString method.

+210
Apr 27 '10 at 23:26
source share

You can use instanceof .

JLS 15.20.2 Typeof instanceof operator

  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[]); // prints "true" 

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.
+61
Apr 27 '10 at 22:15
source share

You can access each element of the array separately using the following code:

 Object o=...; if ( o.getClass().isArray() ) { for(int i=0; i<Array.getLength(o); i++){ System.out.println(Array.get(o, i)); } } 

Note that there is no need to know what underlying array it is, as this will work for any array.

+28
May 09 '14 at 15:25
source share

There is no subtype relationship between arrays of a primitive type or between an array of a primitive type and an array of a reference type. See JLS 4.10.3 .

Therefore, as a test, it is shown below whether obj array of any kind:

 // INCORRECT! public boolean isArray(final Object obj) { return obj instanceof Object[]; } 

In particular, it does not work if obj is a one-dimensional array of primitives. (This works for primitive arrays with higher dimensions, because all types of arrays are subtypes of Object . But in this case, this is a moot point.)

I use Google GWT, therefore I am forbidden to use reflection :(

The best solution (part of the question about the isArray array) depends on what is considered "using reflection".

  • In GWT, calling obj.getClass().isArray() not considered using reflection 1 so this is the best solution.

  • Otherwise, the best way to find out if an object has an array type is to use a sequence of instanceof expressions.

     public boolean isArray(final Object obj) { return obj instanceof Object[] || obj instanceof boolean[] || obj instanceof byte[] || obj instanceof short[] || obj instanceof char[] || obj instanceof int[] || obj instanceof long[] || obj instanceof float[] || obj instanceof double[]; } 
  • You can also try playing obj.getClass() with the class name of the object as follows, but calling obj.getClass() borders on reflection.

     public boolean isArray(final Object obj) { return obj.getClass().toString().charAt(0) == '['; } 



1 - More precisely, the Class.isArray method Class.isArray listed as supported by GWT on this page .

+9
Apr 27 2018-10-22T00:
source share

You can create a utility class to check if the class represents any collection, map, or array

  public static boolean isCollection(Class<?> rawPropertyType) { return Collection.class.isAssignableFrom(rawPropertyType) || Map.class.isAssignableFrom(rawPropertyType) || rawPropertyType.isArray(); } 
0
Jan 22 '19 at 19:24
source share



All Articles