如何在不使用反射的情况下查看对象是否为数组? [英] How to see if an object is an array without using reflection?

查看:49
本文介绍了如何在不使用反射的情况下查看对象是否为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不使用反射,如何在 Java 中查看对象是否为数组?以及如何在不使用反射的情况下遍历所有项目?

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?

我使用 Google GWT,所以我不允许使用反射 :(

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) {
  //??..
}

顺便说一句:我也不想使用 JavaScript 以便我可以在非 GWT 环境中使用它.

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

推荐答案

您可以使用 Class.isArray()

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

这适用于对象和原始类型数组.

This works for both object and primitive type arrays.

对于 toString,请查看 Arrays.toString.您必须检查数组类型并调用适当的 toString 方法.

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

这篇关于如何在不使用反射的情况下查看对象是否为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆