Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see 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 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.

like image 616
edbras Avatar asked Apr 27 '10 22:04

edbras


People also ask

How do you check if an object is an array?

isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false .

How do you check if an object is an array or not in C#?

Use Type. IsArray and Type. GetElementType() to check the element type of an array.

How do you check if it's an array in JavaScript?

The isArray() method returns true if an object is an array, otherwise false .

How do you check if a parameter is an array in Java?

In order to determine if an object is an Object is an array in Java, we use the isArray() and getClass() methods. The getClass() method method returns the runtime class of an object. The getClass() method is a part of the java.


1 Answers

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.

like image 88
Steve Kuo Avatar answered Sep 17 '22 13:09

Steve Kuo