运行结果为什么会显示非法参数异常,我的参数不是定义的Object么
sfsd
Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array
at java.lang.reflect.Array.getLength(Native Method)
at com.heima.test.aa.printSring(aa.java:27)
at com.heima.test.aa.main(aa.java:13)
代码如下:
package com.heima.test;
import java.lang.reflect.Array;
public class aa
{
public static void main(String[] args)
{
String []a4 = new String[]{"aa", "bb", "cc"};
printSring("sfsd");
printSring(a4);
}
public static void printSring(Object obj)
{
Class clazz = obj.getClass();
if(clazz.isArray())
{
int len = Array.getLength(clazz);
for(int i = 0; i < len; i++)
{
System.out.println(Array.get(clazz, i));
}
}
else
{
System.out.println(obj);
}
}
} |