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

前端 未结 6 1804
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 04:08

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 n

6条回答
  •  情深已故
    2020-12-01 04:33

    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.

提交回复
热议问题