在 Java 中打印数组
我正在编写一个方法来打印它传递的每个对象。通过调用对象的 Object.toString()
方法可以正常工作,但不适用于数组。我可以使用 Object.getClass().isArray()
方法找出它是否是一个数组,但我不知道如何转换它。
int[] a;
Integer[] b;
Object aObject = a;
Object bObject = b;
// this wouldn't work
System.out.println(Arrays.toString(aObject));
System.out.println(Arrays.toString(bObject));
I'm writing a method that prints every Object it get passed. This works fine by calling the Object.toString()
method for the object but doesn't works for arrays. I can find out whether it is an Array with the Object.getClass().isArray()
method, but I don't know how to cast it.
int[] a;
Integer[] b;
Object aObject = a;
Object bObject = b;
// this wouldn't work
System.out.println(Arrays.toString(aObject));
System.out.println(Arrays.toString(bObject));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您不知道类型,可以将对象强制转换为
Object[]
并像这样打印它(在确保它确实是一个数组并且可以强制转换为Object[]< /代码>)。如果它不是
Object[]
的实例,则首先使用 reflection 创建一个Object[]
,然后打印:TESTING:
输出:
If you don't know the type you can cast the object to
Object[]
and print it like this (after making sure it is indeed an array and can be cast toObject[]
). If it is not an instance ofObject[]
then use reflection to create anObject[]
first and then print:TESTING:
OUTPUT:
如果您问如何在任何数组上以编程方式调用此方法,那么对于原始数组来说这是不可能的。查看
Arrays
类,您会看到每个基本数组类型都有一个toString()
重载。这是因为
int[]
扩展了Object
而不是Object[]
。编辑:这是一个包含原始数组的冗长解决方案,令人遗憾:
我希望其他人可以提供更优雅的解决方案,但基于原始数组的限制,这可能是您能做的最好的解决方案。
If you're asking how you can call this method programmatically on any array, it's not going to be possible with primitive arrays. Looking at the docs for the
Arrays
class, you'll see there's an overload oftoString()
for every primitive array type.This is because
int[]
for example extendsObject
rather thanObject[]
.EDIT: here's a regrettably longwinded solution to include primitive arrays:
I'm hoping someone else can provide a more elegant solution, but based on the limitations of primitive arrays, this might be the best you can do.
为什么不让 方法重载 解决您的问题?只需编写两个(或多个)具有相同名称但不同签名的方法,然后让编译器决定程序执行时将调用哪个方法:
代码中其他位置的示例用法:
Why don't you let method overloading solve your problem? Simply write two (or more) methods with the same name but different signatures and then let the compiler decide which method will be called when the program executes:
Example usage elsewhere in the code:
如果您不想依赖对象的类本身来重载对象的 toString 方法(以获得比打印类名和哈希码更有用的东西......),则需要使用反射来获取对象的更深层结构
...建议使用一个使用反射来获取更深层次结构的库,例如 GSON:http://code.google.com/p/google-gson/
这可以轻松打印数组,包括多维数组和原始数组。
但真正的好处是,它以统一的方式打印任何类的对象,无论该类是否以任何有用的方式重写了 toString。
You need to use reflection to get to the deeper structure of an Object if you don't want to rely on object's class itself to overload the Object toString method (to get something more useful than printing class name and hash code...)
My suggestion is to use a library that uses reflection to get to the deeper structure, such as GSON: http://code.google.com/p/google-gson/
This prints arrays effortlessly, including multi-dimensional arrays and primitive arrays.
But the real benefit is that it prints objects of any class in a uniform way, whether the class has overriden toString in any useful way or not.
如果您只想将其作为字符串打印出来,为什么要强制转换它呢?只需将其视为对象数组,迭代该数组,然后调用每个对象的 toString() 方法即可。
If all you want to do is print it out as a String, why cast it? Just treat it as an array of Objects, iterate through the array, and the call the toString() method on each Object.