本帖最后由 黑马-王鹏 于 2013-5-9 20:03 编辑
- package cn.itcast.day1;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- public class ReflectDemo1 {
- public static void main(String[] args)throws Exception {
- String[] a4 = new String[]{"a","b","c"};
- printObject(a4);
- }
- private static void printObject(Object obj){
- Class clazz = obj.getClass();
- if(clazz.isArray()){
- int len = Array.getLength(clazz);
- for(int x=0;x<len;x++){
- System.out.println(Array.get(obj,x));
- }
- }else {System.out.println(obj);
- }
- }
- }
复制代码 Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array
at java.lang.reflect.Array.getLength(Native Method)
at cn.itcast.day1.ReflectDemo1.printObject(ReflectDemo1.java:41)
at cn.itcast.day1.ReflectDemo1.main(ReflectDemo1.java:35)
为什么会报出这样的错?
|
|