- public static void main(String[] args) throws Exception {
- char[] c = new char[]{'c','b','q'};
- printer(c);
- }
- public static void printer(Object obj){
- //判断是否为数组类型
- if(obj.getClass().isArray()){
- //用数组的反射类Array得到长度,并打印数组成员
- int len = Array.getLength(obj);
- System.out.print("[ ");
- for(int i=0;i<len-1;i++){
- System.out.print(Array.get(obj, i)+" , ");
- }
- System.out.println(Array.get(obj, len-1)+" ]");
- }else{
- System.out.println(obj);
- }
- }
复制代码 |