117

How can I see in Java if an Object is an array without using reflection? And how can I iterate through all items without using reflection?

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

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

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

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

BTW: neither do I want to use JavaScript such that I can use it in non-GWT environments.

bluish
  • 26,356
  • 27
  • 122
  • 180
edbras
  • 4,145
  • 9
  • 41
  • 78

6 Answers6

289

You can use Class.isArray()

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

This works for both object and primitive type arrays.

For toString take a look at Arrays.toString. You'll have to check the array type and call the appropriate toString method.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
74

You can use instanceof.

JLS 15.20.2 Type Comparison Operator instanceof

 RelationalExpression:
    RelationalExpression instanceof ReferenceType

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

That means you can do something like this:

Object o = new int[] { 1,2 };
System.out.println(o instanceof int[]); // prints "true"        

You'd have to check if the object is an instanceof boolean[], byte[], short[], char[], int[], long[], float[], double[], or Object[], if you want to detect all array types.

Also, an int[][] is an instanceof Object[], so depending on how you want to handle nested arrays, it can get complicated.

For the toString, java.util.Arrays has a toString(int[]) and other overloads 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 //...
}

It's going to be very repetitive (but even java.util.Arrays is very repetitive), but that's the way it is in Java with arrays.

See also

Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • Thanks, it didn't realize it's that simple. Thought insstanceof couldn't be used straightforward with T[] :( – edbras Apr 28 '10 at 07:44
  • 2
    BTW: I also noticed another nice way to discover if something is an array Class.isArray() (used in the Arrays.deepToString()). – edbras Apr 28 '10 at 08:35
  • @edbras: yes, that's what Steve Kuo was saying down below. My solution uses pure linguistic construct instead of API call. – polygenelubricants Apr 28 '10 at 08:38
  • It works fine, I only don't use instanceof but getClass as comparision. Something like: if (array.getClass == int[].class) { Arrays.toString((int[]) array); } Thanks all.. – edbras Apr 28 '10 at 11:05
  • @edbras: That's how `java.util.Arrays` does it, yes. I see that you've been reading the code I linked to. – polygenelubricants Apr 28 '10 at 11:16
  • I prefer Steve Kuo's answer. This answer is not general, you need to know the type of array (example : int) however the other answer is generic. – Mehdi Jan 18 '17 at 19:29
41

One can access each element of an 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));
    }
}

Notice that it is unnecessary to know what kind of underlying array it is, as this will work for any array.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
user1928596
  • 1,503
  • 16
  • 21
  • 2
    `isArray()` was already adequately covered in the answers posted 4 years prior to this one. – Jason C Nov 17 '14 at 04:47
  • 18
    This answer is great because it shows us how to get the size of an array and retrieve an element without knowledge of its content type. I'm sure most people have never written code like this before. – Christopher Yang Jan 13 '15 at 05:40
  • @MaartenBodewes - I would use [this link](http://www.gwtproject.org/doc/latest/RefJreEmulation.html) to decide what "not using reflection" means for GWT. – Stephen C Nov 12 '18 at 23:59
10

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

Therefore, the following is incorrect as a test to see if obj is an array of any kind:

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

Specifically, it doesn't work if obj is 1-D array of primitives. (It does work for primitive arrays with higher dimensions though, because all array types are subtypes of Object. But it is moot in this case.)

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

The best solution (to the isArray array part of the question) depends on what counts as "using reflection".

  • In GWT, calling obj.getClass().isArray() does not count as using reflection1, so that is the best solution.

  • Otherwise, the best way of figuring out whether 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 could also try messing around with the name of the object's class as follows, but the call to obj.getClass() is bordering on reflection.

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

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

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
1

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();
 }
Lucas Pires
  • 1,281
  • 14
  • 21
0

Simply obj instanceof Object[] (tested on JShell).

Sina Madani
  • 1,246
  • 3
  • 15
  • 27