我正在编写一个方法,用于打印传递给它的每个对象。对于对象,可以通过调用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));
if/elseif/else
链中转换为每个原始类型。 - anubhava