黑马程序员技术交流社区

标题: 反射 操作数组 [打印本页]

作者: fmi110    时间: 2015-10-3 09:24
标题: 反射 操作数组
a
  1. package review;

  2. import java.lang.reflect.Array;

  3. public class ReflectArrayDemo {

  4.         /**
  5.          * 通过放射操作数组
  6.          */
  7.         public static void main(String[] args) {
  8. //                int[] obj = new int[]{1,2,3};
  9.                 String[] obj = new String[]{"haha","hehe"};
  10.                 printArr(obj);
  11.         }

  12.         private static void printArr(Object obj) {
  13.                  Class clazz = obj.getClass();
  14.                  if(clazz.isArray()){
  15.                          for(int i = 0;i<Array.getLength(obj);i++){
  16.                                  System.out.println(Array.get(obj, i));
  17.                          }
  18.                  }
  19.         }
  20. }
复制代码







欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2